我正在添加Angular 8路由解析器来预取数据。失败(出于任何原因)后,我将返回EMPTY以取消路线导航。但是,当我对此进行测试时,可以进入catchError块,它显示烤面包机,并返回EMPTY,但不会取消路线导航。根据我读过的所有文章,这应该可行。想知道是否有人看到我看不到的东西。我只包括了resolve方法,因为所有其他配置都可以使用。
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Country[]> | Observable<never> {
return this.countryService.getCountries().pipe(
mergeMap((countries) => {
if (countries)
{
return of(countries);
}
return EMPTY;
}),
catchError(error => {
this.toasterService.pop('error', '', 'There was a problem prefetching the data required to submit a request. If the problem persists, please notify the administrator.');
return EMPTY;
}));
}
我希望每个角度的文档都提供此代码来取消导航。
答案 0 :(得分:0)
更新:我回到了angular.io的源头,并研究了角度小组如何实现分解器。他们有一段代码,所有其他博客文章都缺少:
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Country[]> | Observable<never> {
return this.countryService.getCountries().pipe(
mergeMap((countries) => {
if (countries)
{
return of(countries);
}
this.router.navigate(['/']);//<----- THIS LINE, TO RE-DIRECT TO ANOTHER ROUTE
return EMPTY;
}),
catchError(error => {
this.toasterService.pop('error', '', 'There was a problem prefetching the data required to submit a request. If the problem persists, please notify the administrator.');
this.router.navigate(['/']);//<----- THIS LINE, TO RE-DIRECT TO ANOTHER ROUTE
return EMPTY;
}));
}
所以对我来说,添加路由器重定向似乎是一个额外的步骤。尤其是因为直接矛盾,EMPTY将取消导航。我想念什么?