问题描述
@Selector()
static getStudent(state: StudentModel) {
return (std_id: string) => {
return state.students[std_id];
}
}
在组件中,我试图获取该选择器的快照
let _student = this.store.selectSnapshot(StudentState.getStudent);
console.log('_student : ',JSON.stringify(_student ));
但结果是undefined
感谢任何建议
解决方法
返回的不是值而是函数声明
this.store.selectSnapshot(StudentState.getStudent);
你可能想定义一个 ngxs dynamic selector 它接受参数,比如 std_id,并在选择器主体定义中处理所需的值,并且通过使用动态选择器不定义选择器装饰器。 然后;
this.store.selectSnapshot(StudentState.getStudent(std_id));
快照是静态的,您应该使用@Select 装饰器并在您的组件上订阅它;
@Select(StudentState.getStudent(std_id))
getStudent$: Observable<SomeMOdel>;
ngOnit() {
this.getStudent$.subscribe(console.log);
}
// or
<div *ngIf="getStudent$ | async as student">{{ student }}</div>