1.2.0 • Published 3 years ago

gzh-ioc-decorator v1.2.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

学习使用, 请勿用于生产环境

import IocContainer, { Injectable } from "gzh-ioc-decorator";

// 定义CPU接口
interface CPU {
  calculate(): void;
}
// 定义内存条接口
interface Memory {
 loadApplication(): void;
}

// 定义intel i9 cpu类
@Injectable()
class IntelCPU_I9 implements CPU {
 calculate() {
  console.log('calculate...');
 }
}

// 定义海盗船1t内存条类
@Injectable()
class CORSAIRMemory_1t implements Memory {
 loadApplication() {
  console.log('load application runtime data...');
 }
}

@Injectable()
class Computer {
 private cpu: IntelCPU_I9;
 private memory: CORSAIRMemory_1t;

 constructor(cpu: IntelCPU_I9, memory: CORSAIRMemory_1t) {
  this.cpu = cpu;
  this.memory = memory;
 }

 start() {
  this.memory.loadApplication();
  this.cpu.calculate();
 }
}


const container = new IocContainer()
container.addProvider({ provide: IntelCPU_I9, useClass: IntelCPU_I9 });
container.addProvider({ provide: CORSAIRMemory_1t, useClass: CORSAIRMemory_1t});
const pc = container.inject(Computer)
pc.start();
1.2.0

3 years ago

1.1.0

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago