您应该创建一个执行请求的解析器,将解析器添加到路由并在组件的 onInit 中获取解析的配置数据。
组件.路线:
export const routes: Route[] = [
{ path: ':id', component: Component1, resolve: { data: Comp1Resolver }}
];
Comp1Resolver:
@Injectable()
export class Comp1Resolverimplements Resolve<Config> {
constructor(private configService: ConfigService) { }
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Config> {
return this.configService.get(route.params['id']);
}
}
组件1:
ngOnInit() {
let that = this;
this.route.data.subscribe(data => {
that.config= data['data'];