8

我有一个警卫检查状态是否有令牌。

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”类型上不存在属性“令牌”

4

1 回答 1

13

您在这里犯的错误是您假设 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

于 2018-05-26T16:15:04.017 回答