路由器等待可观察到的完成。确保在使用发出第一个值之后完成
take(1)
first()
. 例如。:
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<IProduct> {
return this.productService.getProduct(route.paramMap.get('id')).pipe(
take(1),
map(product => {
if (!product) {
// noinspection JSIgnoredPromiseFromCall
this.router.navigate(['/404']);
return null;
} else {
return product;
}
})
);
}
顺便说一下,你可以清理你的房间
getProduct
方法如下:
getProduct(guid: string): Observable<IProduct> {
return from(this.firebaseDb.collection('products').doc('XNRkodfbiRoKc2DYPA3o').onSnapshot()).pipe(
map((snapshot:any) => {
const product = <IProduct>snapshot.data();
product.guid = guid;
return product;
}),
);
}