我正在尝试使用注射器在角度中使用可靠性注射。我希望我能够在运行时实例化类型,具体取决于发送此组件的内容。
@Injectable()
export class ServiceInjectionManager {
private _injector: ReflectiveInjector;
constructor() {
this._injector = ReflectiveInjector.resolveAndCreate([
MockBackend,
BaseRequestOptions,
{
provide: Http,
useFactory: (backendInstance: MockBackend, defaultOptions: BaseRequestOptions) => {
return new Http(backendInstance, defaultOptions);
},
deps: [MockBackend, BaseRequestOptions]
},
AppSettings,
HierarchyService
]);
}
public resolve<T extends HierarchyService>(type:any): T {
return this._injector.get(type);
}
}
我似乎无法找到传递类型的方法。我采取了多种方法,包括:
public resolve<T extends HierarchyService>(T): T {
return this._injector.get(T);
}
似乎TypeScript中的泛型与.NET中的泛型不同。