CMR*_*CMR 1 resolver angular-routing angular
我有一个关于在 Angular 4 中路由的问题,尤其是处理数据的问题。
通过实现路由解析器,我的应用将受益匪浅;但是,我正在开发一个复杂的 Web 应用程序,并且在我的一个路由(一个联系页面)中,有几个组件正在呈现,每个组件都有不同的后端服务调用。每个调用都在实现不同的接口。这是由于数据库限制。
他们是我可以为我的每个服务编写 1 个解析器的一种方式,还是我需要为每个实现新接口的服务调用编写一个解析器?
您可以在单个解析器中完成所有操作,如下所示:
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> {
return Observable.forkJoin(
this.someService.apiCallA(),
this.someService.apiCallB(),
this.someService.apiCallC(),
)
.map(([resA, resB, resC]: [ResponseAType, ResponseBType, ResponseCType]) => {
return {
aData: resA,
bData: resB,
cData: resC
};
});
}
Run Code Online (Sandbox Code Playgroud)
然后在您的主页组件中:
constructor(private aRoute: ActivatedRoute) {
aRoute.data.subscribe((data: any) => {
// You can find aData, bData, and cData here inside of data
// Pass them down into the components that need them
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
739 次 |
| 最近记录: |