我对NGXS库有问题。我想使用.selectSnapshot函数从状态中获取一个值,但是尽管我在状态中设置了默认值,但我总是无法定义。也许它与状态的生命周期和2+(在版本9 atm上)的生命周期有关。但是当我使用selectSnapshot或.select和.map来console.log我的状态值时,我仅收到未定义的信息。我在ngOnChanges-method中的指令中使用了select-methods,并且该指令在首页上进行了初始化(也许是在State实际就绪之前进行了初始化?)。 idk,我需要帮助。
this.store.selectSnapshot(state => state.availableData)
this.store.select(state => state.availableData).pipe(map(state => console.log(state)))
我试图等待可观察到的东西
private async getAvailableData(){
return await this.store.select(state => state.availableData).pipe(map(data =>console.log(data))).toPromise();
}
也许与DataState的DataModel有关。所以我也尝试了选择:
this.store.selectSnapshot((state: DataState) => state.availableData) // Throws error that availableData is not part of the DataState, but it is actually part of the DataStateModel.
希望您能帮助我。预先感谢
编辑:// 我也尝试过:
this.store.selectSnapshot(state => state.datastore.availableData) // datastore is the state-name of the DataStore class
Edit2:// 这是一个Stackblitz示例: https://stackblitz.com/edit/ngxs-todo-example-czbmby?file=src%2Fapp%2Ftest.directive.ts
答案 0 :(得分:0)
发现问题后,您必须在@ State-Definition处使用给定的州名称:
所以您必须使用state..propertyoftheState
this.store.selectSnapshot(state => state.datastore.availableData)// datastore is the state-name of the DataStore class
这是正确的,但是我在@ State-Definition中有一个错字...。