- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在学习 Angular 2,我正在尝试使用 ngrx/store,但我在某些特殊情况下遇到了一些困难。
示例我正在尝试删除父对象。我想做的是也删除子对象。
这是我的实体:
export class Discussion {
id: string;
name: string;
createdAt: Date;
posts: Post[];
}
export class Post {
id: string;
title: string;
data: string;
createdAt: Date;
comments: Comment[];
}
export class Comment {
id: string;
data: string;
createdAt: Date;
}
我正在使用 normalizr 来展平我的状态,因此我存储的讨论将如下所示:
{
id: "1",
name: "First dicussion",
createdAt: "...",
posts: ["1", "2", "3", "5"]
}
我有 3 个 reducer,一个用于讨论,另一个用于帖子,最后一个用于评论。所有 reducer 都处理自己类型的 delete Action。这是讨论缩减器的示例:
export function reducer(state = initialState, action: discussion.Actions): State {
switch (action.type) {
case discussion.REMOVE: {
const idToRemove = action.payload;
const newEntities = state.entities;
delete newEntities[idToRemove];
return Object.assign({}, state, {
entities: newEntities
});
}
}}
我的 Action 是这样的:
export class RemoveAction implements Action {
readonly type = REMOVE;
/**
* Constructor
* @param payload The id of the discussion to remove
*/
constructor(public payload: string) { }
}
当我删除一个讨论时,我想删除与该讨论相关的帖子,帖子效果将被删除与已删除帖子相关的评论。我使用了 ngrx 的效果,所以我使用了这个效果:
@Effect()
removeDiscussion: Observable<Action> = this._actions
.ofType(dicussion.REMOVE)
.map((action: discussion.RemoveAction) => action.payload)
.mergeMap(discId => {
// How to get posts from discussion id ???
// Fire related Actions
return [
new posts.RemoveAction(postsToRemove)
];
});
我的问题是如何让帖子从讨论 ID 中删除?
感谢阅读。
最佳答案
您可以使用 withLatestFrom
访问 effect 中的商店.
(导入 'rxjs/add/operator/withLatestFrom';
)
将商店注入(inject)效果类:
constructor(private _actions: Actions, private store: Store<fromRoot.State>)
在效果中使用它:
@Effect()
removeDiscussion: Observable<Action> = this._actions
.ofType(dicussion.REMOVE)
.map((action: discussion.RemoveAction) => action.payload)
.withLatestFrom(this.store, (payload, state) => ({ discId: payload, state }))
.mergeMap(({ discId, state }) => {
// access the posts array of the discussion
const postsToRemove = state.discussions[discId].posts;
// Fire related Actions
return [
new posts.RemoveAction(postsToRemove)
];
});
语法 .mergeMap(({ discId, state }) => ...
被称为 destructuring 。
如果您不喜欢这种语法,可以将其替换为 .mergeMap((payloadAndState) => ...
。然后您可以通过 访问
discId
payloadAndState.discId
关于angular - ngrx/store 效果嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44257339/
我已经建立了这个IStore: export interface IStore { user: IUser; sources: ISourceRedux; } 其中IUser是: export
store.select()发出先前的存储状态。 是否可以订阅从“此刻开始”的更改,而无需获取先前的商店值(value)? 最佳答案 如果您对第一个发出的值不感兴趣,则应该可以使用 skip 运算符:
我已将我的 ngrx 代码更新到版本 8(使用 Action Creator 等),但我现在不知道如何在 NgRx Store DevTools 中使用 Dispatcher。 在我能够发送这样的操作
我正在开发一个新的 angular 4 plus @ngrx 4 项目。 我希望对加载的数据具有搜索功能。 例如,所有联系人信息都已加载到组件中。 联系人列表将过滤与搜索文本匹配的联系人姓名。 Ple
我正在使用 Ngrx 和 Angular2 构建一个移动应用程序。当用户从我的应用程序中注销时,我想清除商店?谁能知道该怎么做? 最佳答案 您应该在每个 reducer 中有一个明确的操作,这将清理商
我看到很多代码示例,其中 store.dispatch() 调用直接发生在 Angular 组件中。让一个愚蠢的 Angular 组件访问整个 Store 不是很糟糕吗?为什么不将所有的 Action
ngrx 的支持者声称 (here for example),您可以并且应该将所有应用程序状态保存在单个 Store 中。这表明 @ngrx/Store 可以用于缓存,因为缓存的内容是一种应用程序状态
我的应用程序在调度某个 Action 时没有调度某些 Action 或某些效果没有被调用,这有问题(请参阅 ngrx effect not being called when action is di
下面的代码片段有什么作用?取自此 file . export const getCollectionLoading = createSelector(getCollectionState, fromC
如果在同一个商店上分派(dispatch)多个操作: store.dispatch(new SomeAction()); store.dispatch(new SomeOtherAction());
我试图了解 typeof 效果在 ngrx 中是如何工作的,如果我在我的应用程序模块中声明: .... @NgModule({ imports: [ EffectsModule
任何人都可以建议在角度应用程序中使用 ngrx 进行状态管理时如何控制台记录状态。我已经浏览了 ngrx-store-logger,但是文档并不清楚如何创建元 reducer 和使用这个库。 最佳答案
我一直在阅读ngrx示例应用程序的代码并找到两个函数调用 createFeatureSelector('auth'); 和 createSelector(selectAuthState,(state:
我正在使用 Angular 8 和 NGRX 8。我有一个操作: export const loadEnvironment = createAction( LicencingActionTypes
我正在使用 Angular 8 和 NGRX 8。我有一个操作: export const loadEnvironment = createAction( LicencingActionTypes
以下示例取自 @ngrx example . 我以这种方式理解这个 observable。第一map函数获取 payload这是要添加的书,再次由mergeMap处理它保存到数据库的位置。 原码:
我目前正在使用 NgRx Data 对我项目中的几个实体执行 CRUD 操作。现在,我必须开发分页。因此,REST API 响应将如下所示: { "page": 1, "per_pag
如何在 NGRX 中访问(读取)另一个 reducer 中的 reducer 状态?这是一个与 this 非常相似的问题.NGRX 是否为此提供任何其他解决方案? 最佳答案 我在考虑做类似的事情时偶然
我正在尝试按属性过滤掉有效负载数据。 //reducer.ts case MessagesActionTypes.LOAD_Message_SUCCESS: { console.lo
我有效果类,我想根据路由器参数 ID 加载详细信息 @Effect() getDetails$ = this.actions$.ofType(DetailActions.GET_DETAILS).
我是一名优秀的程序员,十分优秀!