我正在使用HTTP Patch请求更新一组实体到远程后端.来自后端的响应仅包括更新的实体(即,不是所有实体).
我使用实体状态适配器设置我的reducer并用于updateMany更新我的实体:
case settings.SettingsActionTypes.UpdateSettingsSuccess: {
return {
...state,
...adapter.updateMany(action.payload.map((category) => Object.assign({}, {id: category.name, changes: category})), state),
loaded: true,
loading: false,
}
}
Run Code Online (Sandbox Code Playgroud)
虽然这会更新接收更新的实体,但它会删除后端未返回的所有其他实体.
有没有办法告诉ngrx只更新包含在action.payload?中的实体?
我开始使用ngrx/entity包,在那里我可以通过适配器管理存储。有一种addOne方法我想使用,但它将项目添加到集合的末尾。我想在开头添加一个。你能帮我解决这个问题吗?如何在开头添加项目EntityAdapter。
我如何创建实体适配器:
export const adapter: EntityAdapter<AssetTreeNode> = createEntityAdapter({
selectId: (model: AssetTreeNode) => model.Id
});
Run Code Online (Sandbox Code Playgroud)
减速器看起来像这样:
export function reducer(state: AssetListState = initialState, action: AssetListAction) {
switch (action.type) {
(...)
case ASSET_LIST_ADD_ITEM:
let assetToAdd: AssetTreeNode = Object.assign({} as AssetTreeNode,
action.payload.asset,
{ Id: action.payload.createdAssetId });
return adapter.addOne(assetToAdd, state); <--- I wanna add here at the end.
(...)
default:
return state;
}
}
Run Code Online (Sandbox Code Playgroud) 我想使用@ ngrx / entity从实体映射中按单个ID选择一个实体,或按ID数组选择一个实体数组。
我不希望当实体集合获得新元素或实体项更改时触发组件内部的选择订阅,而我根本没有选择。
当我使用selectEntities选择器,然后从结果中选择ID时,这显然发生在我身上。
那么如何从实体集合中按ID选择1或n个项目?
I wanted to store objects with their methods with @ngrx/entity . Can it cause any problems in application? (Angular 2-7)
mission.class.ts:
import { v4 as uuid} from 'uuid';
export class Mission {
id: string;
title: string;
completed: boolean;
constructor (missionTitle: string, completed?: boolean) {
this.id = uuid();
this.title = missionTitle;
this.completed = completed;
}
complete() {
this.completed = true;
}
}Run Code Online (Sandbox Code Playgroud)
There is the class with method 'complete' mission. I want to save it by @ngrx/entity.
missions.actions.ts:
export enum MissionActionTypes …Run Code Online (Sandbox Code Playgroud)实体适配器支持使用“updateOne”方法调用更新集合。但是,当更新值的键不存在时,这会导致特定实体未被更新。请参阅代码示例。
然而,实体适配器Object.assign()在内部使用which while 是好的并不总是我想要的。我希望它基本上取代有问题的实体。
此外,如果所讨论的对象具有结构 A(见下文)。然后发生了一些任意数据库操作并将更新后的 JSON 返回给前端。reducer 最终会以表单的形式获取这些信息。
const originalValue: MyClass = new MyClass('value 1', 'value 2');
const updatedValueFromServer: MyClass = new MyClass('updated');
Run Code Online (Sandbox Code Playgroud)
Then internally to the entity adapter it looks like
const updateValue = Object.assign(originalValue, updatedValueFromServer);
// which would result in ...
// { key1: 'updated', key2: 'value 2' }
// while I would like
// { key1: 'updated' }
// Or
// { key1: 'updated', key2: null }
Run Code Online (Sandbox Code Playgroud)
case ActionTypes.UPDATE_SUCCESS:
return collectionAdapter.updateOne({
changes: action.payload.alertSubscription,
id: …Run Code Online (Sandbox Code Playgroud) 为什么会出现以下错误以及如何解决此问题。
传递给 selectId 实现的实体返回未定义。您可能应该提供自己的 selectId 实现。传递的实体:
我已经查看了与此相关的另外 2 篇文章,其中一篇包含较大状态的文章不适用,而另一篇则注册了多个 forFeature - 我已经对此进行了一些测试,即使我在之前或之后导入它forRoot(reducers) 在我的 app.module 中,没有什么区别。
我确信我在配置中遗漏了一些明显的东西。
我的配置:
export const orderAdapter: EntityAdapter<IOrder> = createEntityAdapter<IOrder>({
selectId: (order: IOrder) => order._id
});
export interface AllOrdersState extends EntityState<IOrder> {}
export const initialState: AllOrdersState = orderAdapter.getInitialState({
ids: [],
entities: {}
});
export const OrdersState = createFeatureSelector<AllOrdersState>('orders');
export const {
selectIds,
selectEntities,
selectAll,
selectTotal
} = orderAdapter.getSelectors(OrdersState);
Run Code Online (Sandbox Code Playgroud)
我的实际减速器:
export function ordersReducer(
state: AllOrdersState = initialState,
action: actions.OrdersActions
) {
case actions.CREATE_MANY: {
console.log(action.orders);
return orderAdapter.addMany(action.orders, state);
}
}
Run Code Online (Sandbox Code Playgroud)
我在 Order.Module 中注册为:
StoreModule.forFeature('orders', …Run Code Online (Sandbox Code Playgroud) https://github.com/rokudo5262/phone这是我的项目,
我想加载智能表中的品牌列表,但收到警告,
我尝试修复,但警告仍然存在,请帮助
Brands-features.selector.ts
import { createFeatureSelector } from '@ngrx/store';
import { State, FeatureKey } from '../reducers';
export const selectBrandsState = createFeatureSelector<State>(FeatureKey);
Run Code Online (Sandbox Code Playgroud)
品牌.selector.ts
import { createSelector } from '@ngrx/store';
import { selectBrandsState } from './brands-features.selector';
import { BrandsReducer } from '../reducers';
import { brandAdapter } from '../states/brands.state';
export const selectBrandEntitiesState = createSelector(
selectBrandsState,
state => state[BrandsReducer.brandsFeatureKey],
);
export const {
selectIds: selectBrandIds,
selectEntities: selectBrandEntities,
selectAll: selectAllBrands,
selectTotal: selectTotalBrands,
} = brandAdapter.getSelectors(selectBrandEntitiesState);
export const BrandSelectors = {
selectBrandEntitiesState,
selectBrandIds,
selectBrandEntities, …Run Code Online (Sandbox Code Playgroud) 我正在使用@ngrx/entity,这会创建一个如下所示的状态:
{
ids: [1, 2, 3],
entities: [
1: {id:1, title: "Some title", likes: {count: 1}},
2: {id:2, title: "Some title", likes: {count: 1}},
3: {id:3, title: "Some title", likes: {count: 1}}
]
}
Run Code Online (Sandbox Code Playgroud)
@ngrx/entity 确实为我们提供了一些更新项目的酷帮手,但似乎(从我在文档中看到的)仅限于更新整个实体。
但是,当用户切换“喜欢”按钮时,我希望在我的减速器中仅state.entities[2].likes使用响应更新该属性。
关于如何解决这个问题的任何想法?
我希望从我的服务器规范化数据,以便我可以更轻松地将它与 ngrx/entity 一起使用。
我喜欢 ngrx/entity 如何通过提供 EntityState 接口和 EntityAdapter 来降低减速器和其他东西的复杂性。但是我发现它不适用于嵌套数据。
我有 3 个级别的数据:
训练 -> 练习 -> 套
如果我将它与 ngrx/entity 的经典模式一起使用,当我使用嵌套数据时,它会很快变得拥挤。
下面是我在使用 ngrx/entity 时遇到的第一件事
在那之后,我四处窥探并进入了 normalizr 库
我喜欢 normalizr 如何规范化我的数据,并且还用仅 id 作为其他实体(练习、集合)的键替换嵌套数组值
我首先尝试的是组合多个实体状态,如下所示:
但这需要改变我的服务器以及大量的逻辑和努力。
我想要的是以某种方式将 normalizr 与 ngrx/entity 结合起来。 得到 normalizr 给我的同样的东西,但可以自由地使用来自 ngrx/entity 的实体适配器 api 它是选择器和其他代码,这些代码在我的服务中来自 ngrx/entity
所以最重要的是,我的问题是如何在没有某种服务器工作的情况下使用 ngrx/entity(就像 normalizr 库那样)规范化深层嵌套数据。
ngrx-entity ×10
ngrx ×9
angular ×6
ngrx-store ×3
angular8 ×2
ngrx-effects ×1
redux ×1
typescript ×1