【问题标题】:Add previous state to the current state(list) in NgRX在 NgRX 中将先前状态添加到当前状态(列表)
【发布时间】:2020-08-28 17:03:45
【问题描述】:

我一直在尝试解决这部分问题。

export const personReducer = createReducer(
    initialState,

    on(LOAD_ALL_SUCCESS, (state, { list }) => {
        // list.push(state.entities)
        const tmpArr : Person[] = Object.values(state.entities)
        
        tmpArr.forEach(t =>{
            list.push(t)
        })

        return adapter.addAll(list, {
            ...state,
            loaded: true,
            loading: false,
            saved201: false,
            updated200: false,
            error: undefined,
            formState: undefined,
        })
    }),
)

我的目标是将之前的状态添加到我的当前状态,以便我的人员列表包含我将在我的 ui 中显示的之前和当前状态。 这意味着即使我移动到下一页,我也必须显示所有内容。

我以为我可以将 state.entities 从字典转换为数组,但它给了我一个错误。有没有办法实现我的目标?

顺便说一句。在我的 UI 中,我没有使用典型的分页,而是向下滚动(如果滚动到达底部,则显示或提取数据,就像在 facebook 新闻提要中一样)

我正在使用 ngrx v10 https://ngrx.io/

    "@ngrx/effects": "^10.0.0",
    "@ngrx/entity": "^10.0.0",
    "@ngrx/router-store": "^10.0.0",
    "@ngrx/store": "^10.0.0",
    "@ngrx/store-devtools": "^10.0.0",
    "@rxweb/reactive-form-validators": "^2.0.0",

对于选择器

export const selectAll = createSelector(
    selectorPersonFeature,
    adapter.getSelectors().selectAll
)

所以我就这样结束了。

this.store.pipe(select(selectAll))
      .subscribe((p: Person[]) => {
        p.forEach(i => {
          this.person$.push(i)
        })
      })

谢谢,

【问题讨论】:

    标签: javascript angular redux ngrx


    【解决方案1】:

    您还可以使用其他一些方法...
    尝试使用adapter.addMany()
    https://ngrx.io/api/data/EntityCollectionReducerMethods#addMany

    【讨论】:

      猜你喜欢
      • 2018-07-23
      • 2022-01-18
      • 2020-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多