router.navigate不起作用

时间:2017-06-21 11:16:54

标签: angular angular2-routing

我在尝试成功删除数据后尝试重定向页面。所以我把重定向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 }
        );
    });

1 个答案:

答案 0 :(得分: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]) 
    }
相关问题