# vue-di-container

> Dependency injection container for Vue

Latest version **0.1.0** (published 2018-12-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install vue-di-container
pnpm add vue-di-container
yarn add vue-di-container
bun add vue-di-container
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2018-12-16 |
| First published | 2018-12-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 23.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | tsufeki |
| Maintainers | tsufeki |
| Keywords | vue, dependency injection, di, injector, ioc |

## Links

- npm: https://www.npmjs.com/package/vue-di-container
- Repository: https://github.com/tsufeki/vue-di-container
- Homepage: https://github.com/tsufeki/vue-di-container#readme
- Issues: https://github.com/tsufeki/vue-di-container/issues
- npm.io page: https://npm.io/package/vue-di-container

## Dependencies (1)

- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 0.1.0 (latest) — 2018-12-16

## README

vue-di-container
================

Dependency injection container for Vue (especially class components).

Installation
------------

```sh
npm install vue-di-container
```

Register the plugin:

```ts
import { VueDiContainer } from 'vue-di-container';

Vue.use(VueDiContainer);
```

Usage
-----

### Keys

Injectable dependencies are identified by keys. Key is either a class function
or an instance of `Token<T>`. Note that Typescript interfaces will not work.

### Defining services

To define injectable service class, use `@Service` decorator. When container
instantiates it, it will inject all constructor arguments (based on declared
parameter type or key provided with `@Inject` decorator) as well as properties
marked with `@Inject`.

```ts
import { Inject, Service, Token } from 'vue-di-container';

class ServiceA {}
class ServiceB {}
const TOKEN = new Token<number>('token');

@Service()
class ServiceC {

    @Inject() serviceA!: ServiceA;
    @Inject(ServiceB) serviceB!: any;
    @Inject(TOKEN) token!: number;
    foo!: ServiceA; // not injected as not decorated with @Inject

    constructor(
        serviceA: ServiceA, // injected with ServiceA
        @Inject(TOKEN) token: number,
    ) {}
}
```

### Providers

What actually will be injected into services and components is determined by
defined providers. Provider are set in component's options and are
inherited by child components (and only them). Services are constructed lazily,
only when needed.

```ts
@Component({
    diProvide: [
        // Construct instance of ServiceC, inject its dependencies.
        ServiceC,
        // Similar, but register under different key and override some arguments.
        { key: TOKEN, class: ServiceC, args: [ServiceA] },
        // Explicit value.
        { key: TOKEN, value: 7 },
        // Use the result of a function. All arguments must be provided.
        { key: ServiceB, factory: arg => { ... }, args: [TOKEN] },
        // Register existing service under different key.
        { key: TOKEN, aliasOf: ServiceA },
    ],
})
class ComponentA {}
```

Services that need to be available to all components should be set in Vue root:

```ts
new Vue({
    ...
    diProvide: [
        ...
    ],
    render: h => h(App),
}).$mount('#app');
```

### Injecting into components

Only property injection is supported on component classes. Alternatively, there
is an option for this.

```ts
@Component({})
class ComponentB {

    @Inject() serviceC!: ServiceC;
}

// or

@Component({
    diInject: {
        serviceC: ServiceC,
    },
})
class ComponentC {}
```

License
-------

MIT, see [LICENSE]('LICENCE').

---
_Source: https://npm.io/package/vue-di-container · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
