【问题标题】:router.navigate doesn't workrouter.navigate 不起作用
【发布时间】:2018-02-14 18:17:36
【问题描述】:

我在成功删除数据后尝试重定向页面。所以我把重定向 router.navigate 放在数据部分的订阅中。但它不起作用,我也尝试过使用 ngZone 也没有任何反应。我也想显示成功信息。我怎么能证明这一点?

没有 ngZone:

  const rid = params['rid'];
        this.roleSer.deleteRole(rid).subscribe(

            data => { this.router.navigate(['viewroles']) },
            error => { error }
        );
    });

使用 ngZone:

const vrid = this.route.params.subscribe((params: Params) => {
        const rid = params['rid'];
        this.roleSer.deleteRole(rid).subscribe(
            data => { this.zone.run(()=>{
                 this.router.navigate(['viewroles']) 
            }); },
            error => { error }
        );
    });

【问题讨论】:

  • this.zone.run(() 是什么?
  • 感谢您的建议。我努力了。但不工作
  • 你遇到了什么错误?
  • 确保你的路径是正确的?

标签: angular angular2-routing


【解决方案1】:

创建一个重定向函数并使用它

const vrid = this.route.params.subscribe((params: Params) => {
            const rid = params['rid'];
            this.roleSer.deleteRole(rid).subscribe(
                data => { 
                    this.redirect('viewroles');
                },
                error => { error }
            );
        });

    redirect(path): void {
     this.router.navigate(['/' + path]) 
    }

【讨论】:

    猜你喜欢
    • 2018-09-17
    • 1970-01-01
    • 2020-05-21
    • 2013-10-16
    • 2019-09-08
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    相关资源
    最近更新 更多