angular - 如何使用选择快照?
问题描述
我有一个警卫检查状态是否有令牌。
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”类型上不存在属性“令牌”
解决方案
您在这里犯的错误是您假设 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
推荐阅读
- android - LiveData、MVVM 和存储库模式
- docusignapi - 如何在docusign php api的帮助下将html表格添加到DocuSign文档中?
- c++ - QTableWidget 到多个文件
- c++ - OpenGL着色器存储缓冲区/memoryBarrierBuffer
- c# - 如何将组合框绑定到 ObservableCollection 列表中的某个项目 - WPF?
- c++ - 从多个模板化基类派生时出现“隐藏重载的虚函数”警告
- reactjs - 为什么我应该使用 Ant Design 而不是其他前端库
- ios - 将设备升级到 iOS12 后应用程序崩溃
- java - Gradle 在使用 annotationProcessor 时引发错误:以下任务之间的循环依赖关系
- javascript - 材质 ui 日期选择器显示选定值 -1