您没有提供任何代码,因为您的案例应该可以在不需要 cancelAllNavigationRequests 的情况下工作,您只是没有正确处理 HTTP 错误。这是我在警卫中使用的一种方法,它可能会帮助或给你一些想法,它类似于 ngex 示例中的方法:这个警卫将在调用服务之前检查分类是否已经存在并且不是部分的。
@Injectable()
export class ClassifiedGuardService implements CanActivate, CanActivateChild {
constructor(private store: Store<fromRoot.State>, private classifiedService: ClassifiedService, private router: Router) {
}
vendorClassifiedAlreadyLoaded(id: string): Observable<boolean> {
return this.store.pipe(
select(fromContext.selectAllClassifieds),
map(entities => {
for (let i = 0; i < entities.length; i++) {
if (entities[i].id === id && !entities[i].partial) {
return true;
}
}
return false;
}),
take(1)
);
}
getVendorClassifiedFromServer(id: string) {
return this.classifiedService.getVendorsClassified({id: id}).pipe(
map(payload => new ClassifiedAction.GetVendorsClassifiedSuccess(payload)),
tap((action: ClassifiedAction.GetVendorsClassifiedSuccess) => this.store.dispatch(action)),
map(payload => !!payload),
catchError(err => {
this.store.dispatch(new ClassifiedAction.GetVendorsClassifiedFail(err));
this.router.navigate(['/errors/bad-request']);
return of(false);
})
);
}
getFullClassified(id: string): Observable<boolean> {
return this.vendorClassifiedAlreadyLoaded(id).pipe(
switchMap(inStore => {
if (inStore) {
return of(inStore);
}
return this.getVendorClassifiedFromServer(id);
})
);
}
canActivate(route: ActivatedRouteSnapshot): Observable<boolean> {
return this.getFullClassified(route.params['id']);
}
canActivateChild(route: ActivatedRouteSnapshot) {
return this.canActivate(route);
}
}