如果在 Angular 2 中解析失败,我如何重定向到另一个页面? 我为我的编辑页面调用此解析,但我想处理解析页面中的错误
我的决心:
resolve(route: ActivatedRouteSnapshot): Promise<any>|boolean {
return new Promise((resolve, reject) => {
if (route.params['id'] !== undefined) {
this.dataService.getHttpEmpresaShow(this.endpoint_url_Get + route.params['id'])
.subscribe(
result => {
console.log("ok");
return resolve(result);
},
error => {
return resolve(error);
});
}});
最佳答案
就像in the docs ,调用 this.router.navigate(["url"])
...(考虑在构造函数中注入(inject) Router
)
class MyResolve {
constructor(private router: Router) {}
resolve(route: ActivatedRouteSnapshot): Observable <any> {
return this.dataService.getHttpEmpresaShow(this.endpoint_url_Get + route.params['id'])
.pipe(catchError(err => {
this.router.navigate(["/404"]);
return EMPTY;
}));
}
}
关于angular - 如果 Resolve 失败 Angular 2 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43097103/