首页 文章

在Angular 6中选择具有RxJS的状态不起作用

提问于
浏览
0

在Web应用程序中我使用的是Angular和RxJS 6.应用程序有一个登录,登录的响应是一个令牌,因此,在登录后,我将令牌保存在sessionStorage中,然后我解码令牌以保存当前登录用户 . 这是登录后的状态:

enter image description here

我正在尝试开发此行为:如果用户转到页面"..login"(因此在login.components.ts中)并且如果他已经登录,则应用程序将重定向到主页 . 这是我在 login.component.ts 中的ngOnInit()内的代码:

ngOnInit() {
   this.store.select('auth')
     .pipe(take(1),
       map((authState: fromAuth.State) => {
         console.log('test');
         if (authState.authenticated) {
           this.router.navigate(['/home']);
         }
      }));
}

这是AppState的接口:

export interface AppState {
  user: fromProfile.State,
  auth: fromAuth.State,
  core: fromCore.State
}

测试它,我登录,我回家,然后我去登录页面,我在console.log()中放了一个断点,但它没有到达断点,它甚至没有输入它,所以它没有不会重定向到主页 . 为什么?

*** UPDATED *** :这是调试的屏幕
enter image description here

1 回答

  • 0

    select() 函数返回 Observable 所以你必须 .subscribe() 才能调用 pipe .

相关问题