[英]router.navigate() doesn't redirect inside .subscribe
I have a login function like so:我有一个像这样的登录功能:
login() {
// clear error text
this.state.errorText = "";
// try to login
this.sb.login(this.model.username, this.model.password).subscribe(x => {
// check if ok
if (x.success) {
console.log('does this print') //<-- outputs as expected
this.router.navigate(['/app']);
} else {
this.state.errorText = x.response;
}
});
}
Through debugger I can see the x.success comes back as true.通过调试器,我可以看到 x.success 返回为真。 My console.log prints but after, the this.router.navigate(...) line doesn't seem to do anything.
我的 console.log 打印但之后, this.router.navigate(...) 行似乎没有做任何事情。 there is no redirect.
没有重定向。
I've tried to use:我试过使用:
this.zone.run(() => this.router.navigate(['/app']));
but that results in the same, no page redirect.但这会导致相同的结果,没有页面重定向。
1 Assign Route Service Locally 2 Use the Assigned Local Service 1 在本地分配路由服务 2 使用分配的本地服务
login() {
this.state.errorText = "";
var route = this.router; // Assign router locally
// try to login
this.sb.login(this.model.username, this.model.password).subscribe(x => {
// check if ok
if (x.success) {
console.log('does this print') //<-- outputs as expected
route.navigate(['/app']); // use local route service
} else {
this.state.errorText = x.response;
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.