簡體   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