@muxiu1997/vue-easy-di
TypeScript icon, indicating that this package has built-in type declarations

0.1.0-rc.2 • Public • Published

vue-easy-di

A Vue composable library for simplifying dependency injection.

Install

$ npm install @muxiu1997/vue-easy-di
import defineEmitterComposable from '@muxiu1997/vue-easy-di'

Usage

// Defining a dependency injection composable
const useMyDependency = defineUseDependencyInjection<MyType>(() => newMyType());

// Providing a value at the parent component
const parentComponent = defineComponent({
  setup() {
    const myDependency = useMyDependency('provide');

    // Or overrides default initializer
    const myDependency = useMyDependency('provide', () => newMyType());
  }
});

// Injecting a value in a child component
const childComponent = defineComponent({
  setup() {
    const myDependency = useMyDependency('inject');

    // Or omit 'inject' because it is the default
    const myDependency = useMyDependency();
  }
});

// Using with default value
const useDefaultDependency = defineUseDependencyInjection<MyType>({ injectDefault: () => newMyType() });
// Using with default value in inject mode
const myDependency = useDefaultDependency('inject', { injectDefault: () => newMyType() });

// Throwing error when no provider is found
const useStrictDependency = defineUseDependencyInjection<MyType>({ throwOnNoProvider: () => new Error('No provider found for useStrictDependency') });
// Throwing error when no provider is found in inject mode
const myDependency = useStrictDependency('inject', { throwOnNoProvider: () => new Error('No provider found for myDependency') });

License

MIT

Package Sidebar

Install

npm i @muxiu1997/vue-easy-di

Weekly Downloads

2

Version

0.1.0-rc.2

License

MIT

Unpacked Size

69.1 kB

Total Files

10

Last publish

Collaborators

  • muxiu1997