如果 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);
});
}});
就像在文档中 https://angular.io/docs/ts/latest/guide/router.html#!#resolve-guard, 呼叫this.router.navigate(["url"])
...(想注入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;
}));
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)