如何引用静态类的方法供应商? [英] How to reference a provider in static class methods?
问题描述
还没有在目前的code,但我一直在努力与喷油器
的喜欢这里。当我 resolveAndCreate()
服务(尝试过其他方法注射过......)新的实例被创建,bootstraped实例被覆盖。
Don't have any code at the moment, but I've been trying standard/boilerplate syntax with Injector
like here. When I resolveAndCreate()
a service (tried other Injector methods too...) new instance is created and bootstraped instance is overwritten.
拟用例是 @CanActivate()
装饰。我一直在做可怕的解决方法( - :设置窗口[__ __准备当服务就绪,并使用
,在装饰...
Intended usecase is with @CanActivate()
decorator. I've been doing awful workaround (-: setting window["__ready__"]
when service is ready and using that in decorator...
推荐答案
的一个解决方案是存储在专用单在引导创建的喷射器的引用。
One solution is to store a reference to the injector created at bootstrap in a dedicated singleton.
bootstrap(App, [Auth, ROUTER_PROVIDERS])
.then((appRef: ComponentRef) => {
// store a reference to the injector
appInjector(appRef.injector);
});
在单如下:
let appInjectorRef: Injector;
export const appInjector = (injector?: Injector):Injector => {
if (injector) {
appInjectorRef = injector;
}
return appInjectorRef;
};
然后你就可以像访问服务:
And then you can access the service like that :
let injector: Injector = appInjector(); // get the stored reference to the injector
let auth: Auth = injector.get(Auth); //get the service from the injector
下面是一个Plunker在那里你可以在行动中看到这一点。 (我不是plunker的作者,但是这是真的对我很有帮助): HTTP :?//plnkr.co/edit/SF8gsYN1SvmUbkosHjqQ p = preVIEW
Here is a Plunker where you can see this in action. (I am not the author of the plunker, but this was really helpful to me) : http://plnkr.co/edit/SF8gsYN1SvmUbkosHjqQ?p=preview
这篇关于如何引用静态类的方法供应商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!