3

我正在尝试使用注入器在角度中使用依赖注入。我希望我能够在运行时根据发送的组件来实例化类型。

@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 中的不同。

4

0 回答 0