qzx-ioc
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

Qzx-Ioc

简单的依赖注入的功能,其实就是自动的帮你创建实例。

安装

npm install qzx-ioc

使用

User.ts

import { Injectable } from 'qzx-ioc';
@Injectable
export class User {
    private name: string;
    getName() {
        return this.name;
    }
    setName(name: string) {
        this.name = name;
    }
}

Main1.ts

import { Ioc } from 'qzx-ioc';
let user: User = Ioc(User);
user.setName('jack');
console.log(user.getName()); // jack

Main2.ts

import { Ioc } from 'qzx-ioc';
let user: User = Ioc(User);
console.log(user.getName()); // jack

可以看到这里使用的是同一个实例。

作为构造函数的参数

除了直接引用,也可以像Angular2里面在构造函数里进行依赖注入。

test.ts

import { Injectable } from 'qzx-ioc';
import { User } from './user';
@Injectable
export class Test {
    constructor(
    	private user: User
    ) {
        
    }
    show() {
        this.user.getName();
    }
}

Main.ts

import { Ioc } from 'qzx-ioc';
let test: Test = Ioc(Test);
test.show(); // jack

Readme

Keywords

Package Sidebar

Install

npm i qzx-ioc

Weekly Downloads

1

Version

2.0.0

License

ISC

Unpacked Size

3.99 kB

Total Files

5

Last publish

Collaborators

  • zxqian1991