我创建了一个解析器,除了在解析器完成运行后所有组件都会加载,但有时会在解析器功能完成之前加载组件。
解析器代码为:
import { Injectable } from '@angular/core';
import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs/Rx';
import { ApiService } from '../services';
import { AppState } from 'spotlightcloud-angular-common-website';
import { Locale } from '../locale/locale';
@Injectable()
export class MetricsLocaleResolver implements Resolve<any> {
constructor(public api: ApiService, public appState: AppState, private locale: Locale) {
}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> | Promise<any> | any {
return this.api.get('monitoring', 'uiconfiguration/metricsmetadata', {}).subscribe(
localeData => {
console.log('localeData: ', localeData);
this.locale.setMetricsLocaleObj(localeData);
}
);
}
}
localeData
打印有时会出现在组件中的打印之后(onChanges
生命周期中)。
答案 0 :(得分:1)
更改
return this.api.get('monitoring', 'uiconfiguration/metricsmetadata', {}).subscribe(
到
return this.api.get('monitoring', 'uiconfiguration/metricsmetadata', {}).map(
如果您致电.subscribe()
,则返回Subscription
,路由器需要Observable
,Promise
或boolean
值。
回调也应该返回一个布尔值
localeData => {
console.log("localeData:",localeData);
this.locale.setMetricsLocaleObj(localeData);
return true;
}