首页 > 解决方案 > Ngrx:如何将 simpe store 模式与实体状态结合起来?

问题描述

最近很抱歉,如果它与某人的问题重复。但是我找不到我的问题的解决方案。

我遇到了一个问题,我想让我的商店状态有点复杂,ngrx/entity提供但我不知道如何正确地做到这一点。

以下是我现在拥有的减速器模型:

export interface State extends EntityState<BagParametres> {
    SimpleBag: BagParametres;
}

export const adapter: EntityAdapter<BagParametres> = createEntityAdapter<BagParametres>({
    selectId: (params: BagParametres) => params.id
});

export const initialState: State = adapter.getInitialState({
    SimpleBag: defaultParams,
    RareBags: {
        ids: [],
        entities: []
    }
});

所以我期望什么商店:

{
    SimpleBag: {
        //params
    },
    RareBags: {
        ids: [2, 3, 4 //, ...etc],
        entities: [
            { id: 2 //, etc params },
            { id: 3 //, etc params },
            { id: 4 //, etc params }
            // ... and so on
        ]
    }
}

我要买什么商店:

{
    SimpleBag: {
        id: 1
        // etc params
    },,
    RareBags: {
        ids: [],
        entities: []
    },
    ids: [2, 3, 4 //, ...etc],
    entities: [
        { id: 2 //, etc params },
        { id: 3 //, etc params },
        { id: 4 //, etc params }
        // ... and so on
    ]

}

如何在其中放置 id 和实体RareBags

标签: angulartypescriptngrxngrx-entity

解决方案


您正在混淆状态。你想要的是 2 个不同的实体存储。

export interface State {
    simpleBag: BagParametres;
    rareBag: RareBagState
}

export interface RareBagState extends EntityState<BagParametres> {}

export const rareAdapter: EntityAdapter<BagParametres> = createEntityAdapter<BagParametres>({
    selectId: (params: BagParametres) => params.id
});

export const initialState: State = {
    simpleBag: defaultParams,
    rareBag: rareAdapter.getInitialState({})
};

推荐阅读