在使用NGXS @Select装饰器时,访问状态模型上定义的属性的正确方式是什么。
例如,定义了以下状态:
export interface UserStateModel {
firstname: string;
lastname: string;
}
@State<UserStateModel>({
name: 'user',
defaults: {}
})
export class UserState {..}在组件中,如果我想像这样选择用户状态:
..export class MyComponent {
@Select(UserState) user$: Observable<UserState>;
ngOnInit(){
this.user$.subscribe(u => {
//do something with user state
console.log(u.firstname);
});
}
}我得到了typescript错误,因为UserState上不存在firstname属性(因为它是在相关模型类型上定义的)。如果我引用的是组件html模板中的属性,那么我没有任何问题。
有一个关于选择器用法的related discussion,但我只是想确认一下我对当前版本的期望(如果我这样做是正确的!)。
我正在使用"@ngxs/store": "^3.0.0-rc.2",
发布于 2019-08-14 20:14:13
您对问题的评论是选择整个状态容器的正确方法。
另一种选择(在我看来是更好的解决方案)是将a root-state model to infer types in Selectors.与那些您可以只选择必要的状态属性并仍然具有类型安全性的属性一起使用。
我对当前发布的解决方案的看法如下:
https://stackoverflow.com/questions/50110208
复制相似问题