繁体   English   中英

router.navigate() 不会在 .subscribe 内重定向

[英]router.navigate() doesn't redirect inside .subscribe

我有一个像这样的登录功能:

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

通过调试器,我可以看到 x.success 返回为真。 我的 console.log 打印但之后, this.router.navigate(...) 行似乎没有做任何事情。 没有重定向。

我试过使用:

this.zone.run(() => this.router.navigate(['/app']));

但这会导致相同的结果,没有页面重定向。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM