我正在尝试将我现有的代码迁移到1.3到2.我有一个配置服务,在应用程序被引导之前加载。另外我需要显示一个使用httpInterceptor的微调器。我正在订阅App组件中的SpinnerService Observable。但是当http调用在bootstraping之前时,observable总是未定义的。
@NgModule({
imports: [BrowserModule, FormsModule, HttpModule, RouterModule,SharedModule, AppRoutingModule
providers: [
{
provide: Http,
useFactory: (backend:XHRBackend, options:CustomOptions) => {
return new HttpService(backend, options, new SpinnerService());
},
deps: [XHRBackend, RequestOptions]
},
SpinnerService ,
ConfigService,
{
provide: APP_INITIALIZER,
useFactory: (config: ConfigService) => () => config.loadConfig(),
deps: [ConfigService],
multi: true
},
{
provide: RequestOptions,
useClass: CustomOptions
},
],
declarations: [AppComponent ],
bootstrap: [AppComponent]
})
app.component.ts中的方法
private createServiceSubscription() {
this.subscription = this.spinnerService.spinnerObservable.subscribe(show => {
if (show) {
this.startSpinner();
} else {
this.stopSpinner();
}
});
}
Spinner服务: 导出类SpinnerService {
public spinnerObserver: Observer<boolean>;
public spinnerObservable: Observable<boolean>;
constructor() {
this.spinnerObservable = new Observable<boolean>((observer: Observer<boolean>) => {
this.spinnerObserver = observer;
}
).share();
}
showSpinner() {
if (this.spinnerObserver) {
this.spinnerObserver.next(true);
}
}
hideSpinner() {
if (this.spinnerObserver) {
this.spinnerObserver.next(false);
}
}
有没有办法在prebootstrap中使用customHttpService显示微调器
}
答案 0 :(得分:0)
使用BehaviorSubject。 BehaviorSubject会发出一个初始值,订阅者将立即获得。
//private
this.spinnerObserver = new BehaviorSubject<boolean>(false);
//public
this.spinnerObservable = this.spinnerObserver.asObservable().share();