简体   繁体   English

使用组件存储选择和更新 NgRx 实体的附加属性

[英]Select and update the additional property of NgRx entity using component store

I have the below additional property我有以下附加属性

export interface MetricCreateState extends EntityState<UserSelectionListDto> {
    isLoading: boolean | undefined,
    isCreated: boolean | undefined,
    LoadConfigureMetric: boolean | undefined
}

Created a adaptor and initial property创建了一个适配器和初始属性

export const adapter = createEntityAdapter<UserSelectionListDto>();
export const { selectAll, selectEntities } = adapter.getSelectors();

export const initialState: MetricCreateState = adapter.getInitialState({
    isLoading: false,
    isCreated: false,
    LoadConfigureMetric: false
});

I have a effect which load the data from the server side as我有一个从服务器端加载数据的效果

readonly getMetricOwners = this.effect((owners$: Observable<UserSelectionListDtoPagedResultDto>) => {
        this.setState((state) => adapter.setAll([], { ...state, isLoading: true }));
        return owners$.pipe(() => this.userServiceProxy.getUsersForSelection(undefined, undefined, undefined, undefined, undefined).pipe(
            tap({
                next: (owners) => {
                    this.setState((state) => adapter.setAll(owners.items, { ...state, isLoading: false }));
                },
                error: (e) => {
                    this.setIsLoading(false);
                },
            }),
            catchError(() => EMPTY),
        ));
    });

Over here I am trying to update the single property在这里,我正在尝试更新单个属性

this.setState((state) => adapter.setAll([], { ...state, isLoading: true }));

Is this is the correct way to set the additional property of the entity?这是设置实体附加属性的正确方法吗? and how do we select the particular additioanl property.以及我们如何选择特定的附加属性。 For example例如

  • I want to update only isLoading我只想更新isLoading
  • I want to select only isLoading我只想选择isLoading

Not really, setAll also updates the entities collection不是真的, setAll也会更新实体集合

Replace current collection with provided collection -docs用提供的集合替换当前集合 -文档

If you just want to update isLoading , you don't need to adapter because the adapter only handles the entities.如果您只想更新isLoading ,则不需要适配器,因为适配器只处理实体。

To update isLoading , you can simply do the following要更新isLoading ,您只需执行以下操作

{ ...state, isLoading: true }

or use the patch method of ngrx component store或者使用ngrx组件存储的patch方法

  this.componentStore.patchState({isLoading: true});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM