我尝试在成功删除数据后重定向页面。所以我将重定向 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 }
);
});
最佳答案
创建一个重定向函数并使用它
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])
}
关于angular - router.navigate 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44674587/