首页 文章

Angular2路由器登录时无法导航

提问于
浏览
1

我很困惑为什么路由器在成功登录后没有导航到请求的路由 . 控制台输出确实显示用户应该被发送到我点击AuthGuard拒绝的最后一个URL,但我订阅中的router.navigate行似乎没有做这个导航 .

onLogin() {
    if (this.formCredentials.valid) {
      this.isLoading = true;
      this.auth.login(this.formCredentials.value.username, this.formCredentials.value.password)
        .subscribe(
            data => {
              localStorage.setItem('id_token', data.access_token);
              localStorage.setItem('refresh_token', data.refresh_token);
              this.auth.requireLoginSubject.next(false);
              console.log(this.auth.lastUrl);
              this.router.navigate([this.auth.lastUrl]);
            },
            error => {
              console.log(error);
              var err = error.json();
              localStorage.removeItem('auth_error');
              localStorage.setItem('auth_error', JSON.stringify(err));
              this.router.navigate(['/unauthorized']);
              this.isLoading = false;
            },
            () => {
              this.isLoading = false;
            }
          );
    }
  }

1 回答

相关问题