Angular 2 RC 5全球供应商
我如何拥有一个初始化一次的全球供应商? 所以我有以下提供商Angular 2 RC 5全球供应商
@Injectable()
export class ApiRequest {
http: Http;
constructor(@Inject(Http) http) {
console.log('Test');
}
}
然后共享模块
@NgModule({
imports: [BrowserModule,
HttpModule],
declarations: [ControlMessage, InfiniteScroll],
entryComponents: [ControlMessage],
providers: [ApiRequest],
exports: [ControlMessage, InfiniteScroll],
})
出口类SharedModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: SharedModule,
providers: [ApiRequest]
};
}
的代码工作,这里的问题是,ApiRequest构造每次我改变路线时都会初始化,所以每一页都会改变。我怎样才能让ApiRequest提供者在整个应用程序中初始化一次?
所以这里的问题是我在一个子模块中声明提供者。即使很难,我只在子模块中使用提供程序,但它在每次注入时仍在初始化。所以我不得不在主模块中声明它,它按预期工作。
这应该只是当模块被延迟加载时的一个问题。您可能需要使用http://stackoverflow.com/questions/39447443/angular-2-difference-between-core-and-feature-modules –
中提到的核心模块。试过这个问题。除非我在主模块中声明它,否则如果模块是或不是惰性加载的,我会得到相同的结果。 – keepwalking
声音就像模块是懒加载的。在这种情况下,您需要实现'forRoot()'https://angular.io/docs/ts/latest/guide/ngmodule.html,https://angular.io/docs/ts/latest/cookbook/rc4- to-rc5.html –
@GünterZöchbauer也尝试过。同样的问题。编辑该问题使用forRoot – keepwalking
我想我看到了这样的问题。这应该在主版本中修复并包含在下一版本中。 –