从NGRX选择器获取数据



我尝试从ngrx选择器作为数组使用此代码

categorys$ = this.store.select(getCategorysSelector)

但是我可以得到一个存储对象然后把结果打印到控制台得到这个

Store {actionsObserver: ActionsSubject, reducerManager: ReducerManager, source: Store, operator: ƒ}

提示:我的数据应该是这样的

1: {_id: '6294cbf9cdd093df47a24d01', name: 'asdasdasd', __v: 0} 2: {_id: '629730d1527c83ae22bc8c62', name: 'asdasd', __v: 0} 3: {_id: '6297311a527c83ae22bc8c65', name: 'asdasd', __v: 0} 4: {_id: '62973120527c83ae22bc8c67', name: 'asdasd', __v: 0}

所以我怎么能得到我的数据作为数组??

categorys$ = this.store.select(getCategorysSelector)

将您从选择器接收的Observable分配给您的categorys$变量。

为了实际访问该选择器中的数据,你需要消费Observable,要么通过在html代码中通过async管道(https://angular.io/api/common/AsyncPipe)订阅它

<ng-container *ngFor="let category of categorys$ | async">
... do something here
</ng-container>

或者在你的ts代码中显式订阅你的Observable:

const categorySubscription = this.categorys$.subscribe(categories => {
// do something here
});

如果您直接订阅typescript,请务必取消订阅OnDestroy

this.store.subscribe(state=>{
this.getValue(state)
})
}

getValue(value:any){
this.permission = value.user.permission}

最新更新