依赖注入 问题
来源:4-1 依赖性注入

_Minos
2018-04-08
class Person {
id: Id;
address: Address;
constructor (@Inject(Id) id, @Inject(Address) address) {
this.id= id;
this.address= address;
}
}
class Id {
static getInstance(type: string):Id {
//设置
return new Id();
}
}
class Address {
province: string;
city: string;
districe: string;
street: string;
constructor(province,city,districe,street) {
this.province= province;
this.city= city;
this.districe= districe;
this.street= street;
}
}
----------------------------------------------------
export class AppComponent {
// squareState: string;
darkTheme = false;
constructor(private oc: OverlayContainer) {
const injector = ReflectiveInjector.resolveAndCreate([
{provide: Person, useClass: Person},
{provide: Address, useFactory: () => {
if( environment.production ) {
return new Address('北京','北京','昌平','回龙观');
} else {
return new Address('西藏','拉萨','xxx','xxx');
}
}},
{provide: Id, useFactory: () => {
return Id.getInstance('idCard');
}},
]);
const person= injector.get(Person);
console.log(JSON.stringify(person));
}
求指教问题及解决办法
1回答
-
接灰的电子产品
2018-04-09
把依赖的类移动到头部
00
Angular打造企业级协作平台,让你在Angular领域中出类拔萃
998 学习 · 536 问题
相似问题