加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – Ngxs – 如何使用selectSnapshot?

发布时间:2020-12-17 17:34:30 所属栏目:安全 来源:网络整理
导读:我有一个警卫,检查状态是否有令牌. canActivate(): boolean {const token = this.store.selectSnapshot((state: AuthenticationState) = state.token); if (!token) { return true; } this.router.navigate(['home']); return false;} 然后我有这样的事情:
我有一个警卫,检查状态是否有令牌.

canActivate(): boolean {
const token = this.store.selectSnapshot((state: AuthenticationState) => state.token);
  if (!token) {
    return true;
  }

  this.router.navigate(['home']);
  return false;
}

然后我有这样的事情:

export class AuthenticationState {
  @Selector()
  static token(state: AuthenticationStateModel) {
    return state.token;
  }
}

我收到一个错误.
‘AuthenticationState’类型上不存在属性’token’

解决方法

你在这里犯的错误是你假设lambda的state参数是你的AuthenticationState,它实际上是整个应用程序状态,它是AuthenticationState的父类.您应该像这样传递您的选择器:

canActivate(): boolean {
const token = this.store.selectSnapshot(AuthenticationState.token);
  if (!token) {
    return true;
  }

  this.router.navigate(['home']);
  return false;
}

几天前,NGXS的作者发表了一篇关于这个确切主题的帖子:
https://medium.com/@amcdnl/authentication-in-ngxs-6f25c52fd385

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读