【发布时间】:2020-09-10 10:04:16
【问题描述】:
在我的组件中的 Angular 应用程序中,我使用从两个 API 获取数据的 switchMap 对两个 Observables 进行了两次订阅,当组件打开时,我必须显示骨架或加载器,我会阻止它,所以我刚刚阅读了Resolve,据我所知,它预加载了数据,然后激活了路由。
所以我想知道如何将以下订阅从 ngOnInit 移动到解析器
这是我的 ngOnInit 现在的样子
ngOnInit(): void {
this.menuService
.menu(this.idNegozio, this.piva, 'IT')
.pipe(
switchMap((menu) =>
forkJoin([
of(menu),
this.pluService.plu(this.idNegozio, this.piva, 'IT'),
])
)
)
.subscribe(([menu, plus]) => {
this.menu = menu;
this.plus = plus;
this.filterPlu(menu[0].id);
if (this.filteredPlu.length) {
this.ready = true;
}
});
}
同样的组件用于三个路线:
{
path: 'asporto',
component: ProductsComponent,
canActivate: [ItemsGuardService]
},
{
path: 'takeaway',
component: ProductsComponent,
},
{
path: 'ecom',
component: ProductsComponent,
},
【问题讨论】:
标签: angular