微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

角度 NGXS:如何使用带有惰性选择器的 selectSnapshot

如何解决角度 NGXS:如何使用带有惰性选择器的 selectSnapshot

我正在学习 NGXS,我有一个惰性选择器,如下所示

@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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。