如果 Resolve 失败,则重定向 Angular 2 [英] Redirect if Resolve fails Angular 2

查看:24
本文介绍了如果 Resolve 失败,则重定向 Angular 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在 Angular 2 中解析失败,我如何重定向到另一个页面?我为我的编辑页面调用此解决方案,但我想处理解决页面中的错误

How Can I redirect to another page if the resolve fails in Angular 2? I call this resolve for my Edit Page, but I want to handle the errors in the Resolve Page

我的决心:

 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);
            });
    }});

推荐答案

就像 在文档中,调用 this.router.navigate(["url"])...(考虑注入 Router你的构造函数)

Just like in the docs, calling this.router.navigate(["url"])... (think to inject Router in your constructor)

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;
      }));
  }
}

这篇关于如果 Resolve 失败,则重定向 Angular 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆