vue-typescript-inject

Angular-like injector for Vue
1,469 13
Install
npm install vue-typescript-inject
Latest Version:0.3.1
Vue:2
License:MIT
Last Updated:Jan 7, 2023
Links: GitHub  ·  npm
Maintainer: springnyan

vue-typescript-inject

npm

Angular-like injector for Vue

Requirements

TypeScript with --experimentalDecorators and --emitDecoratorMetadata flags

reflect-metadata

vue-class-component

Usage

import "reflect-metadata";

import Vue from "vue";
import Component from "vue-class-component";
import VueTypeScriptInject, { injectable, inject } from "vue-typescript-inject";

Vue.use(VueTypeScriptInject); // register vue-typescript-inject

@injectable() // identify service class
class ServiceA {
  public num = 0;

  public increase() {
    this.num += 1;
  }
}

@injectable() // identify service class
class ServiceB {
  constructor(private readonly _serviceA: ServiceA) {} // will be auto injected

  public get str() {
    return "" + this._serviceA.num;
  }
}

@Component({
  template: `
    <div>
      <span>{{ getNum() }}</span>
      <button @click="increase()">Increase</button>
    </div>
  `,
  providers: [ServiceA, ServiceB] // register service providers
})
class ComponentA extends Vue {
  @inject() private readonly _serviceA!: ServiceA; // same as @inject(ServiceA)
  @inject(ServiceB) private readonly _serviceB!: ServiceB;

  public increase() {
    this._serviceA.increase();
  }

  public getNum() {
    return this._serviceA.num;
  }

  public getStr() {
    return this._serviceB.str;
  }
}

For more example, see test/test.ts

For references, see Angular Dependency Injection

License

MIT

Related Packages

vite-plugin-favicons-inject

A Vite plugin for creating and injecting favicons during the application build!

533,525 10
@fonderie/vue

Shared Vue 3 provide/inject for Fonderie — provide one FonderieClient at the roo...

3,963 5
vue-inline-svg

Replace SVG images with inline SVG element

4,535,739 188
vue-ctx-injector

A tool for injecting standalone Vue.js components into HTML contexts.

10,523 0
@goede/nuxt-mail-nightly

Adds email sending capability to a Nuxt.js app. Adds a server route, an injected...

1,180 15

Version History

Version Released Vue License
0.3.1 ^2.6.12 MIT
0.3.0 ^2.6.10 MIT
0.2.1 ^2.5.16 MIT
0.2.0 ^2.5.16 MIT
0.1.3 ^2.5.11 MIT