【问题标题】:ngrx adpter updateOnengrx 适配器 updateOne
【发布时间】:2020-11-30 12:56:58
【问题描述】:

我对 ngrx 适配器有疑问, 如何根据 action 中的参数更新 reducer?

我试图从 rxjs 创建一个 find 方法,但出现“重叠”错误。

主界面和状态界面共享一个关键参数,但不是id。

这是一段代码:

行动:

export const verifyDocument = createAction('[Document/API] Verify Document', props<{ status: Status }>());

减速机:

const _documentReducer = createReducer(
    initialState,
      on(DocumentActions.upsertDocuments, (state, { documents }) => {
        return adapter.upsertMany(documents, state);
      }),
      on(DocumentActions.verifyDocument, (state, { status }) => {

        const records = {...state.entities}


        console.log("reducer: ", records, status, "end.");
        return state;
        //return adapter.updateOne(???, state);

【问题讨论】:

    标签: angular rxjs ngrx


    【解决方案1】:

    您可能想尝试通过以下方法实现它:

    首先创建一个selector,这样你就可以通过所需的属性获取对象:

    export const getPersonByName = createSelector(getPeopleEntities, (people: Person[], name: string) => people.find(x = x.name == name));
    

    使用Effect 会监听UpdateByNameAction 并依次发送UpdatePesonByIdAction 将允许您的预期行为:

    public findByName$ = createEffect(() => this.actions$
     .pipe(
        ofType(SchemaActionTypes.FindByName),
        switchMap(name => this.store.pipe(select(getPersonByName, name))),
        map((person) => authAction.updatePesonByIdAction(person, person.id))
      )
    );
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    相关资源
    最近更新 更多