我有以下 reducer :
case ProductionCommentActionTypes.EditCommentSuccess: {
return adapter.updateOne(action.payload, state);
}
有效负载是以下对象:
{
id: number,
changes: {
id: number;
avatar: string;
createdAt: Date;
createdBy: string;
body: string;
discipline: string;
isConcern: boolean;
isResolved: boolean;
plan_Id: number;
resolution: {
id: number;
comment_Id: number;
avatar: string;
createdAt: Date;
createdBy: string;
body: string;
}
}
}
不幸的是,当我更新对象时,我可以清楚地看到 action.payload 具有我预期的数据。我检查了 action.payload 和效果:
@Effect()
resolveProductionConcernSuccess$: Observable<Action> = this.actions$
.ofType<ResolveConcernSuccess>(ProductionCommentActionTypes.ResolveConcernSuccess).pipe(
// Performing a get rather than an edit as the comment should have already
// been updated in the service when the resolution was added.
switchMap(action => this.commentService.getComment(action.payload.comment_Id)),
map((comment: any) => {
comment.updatedBy = "test";
return new EditCommentSuccess(comment);
}),
catchError(err => of(new EditCommentFail(err)))
);
然而,我从中看到的是:
this.comments$ = rootStore.select<Comment[]>(fromRoot.getCommentsForSelectedPlan);
this.comments$.subscribe(res => console.log(res));
是旧数据,即使触发了更改。我至少希望看到 updatedBy = "test"。如果我在调试器中单步执行,我还会在 updateOne 之后立即看到有关状态的旧数据。
另一种说法是
return adapter.updateOne(action.payload, state);
尽管有效载荷中的值正确,但似乎实际上没有改变任何东西。
为什么状态上的属性实际上没有更新?有正确的方法吗?
最佳答案
问题出在 reducer 上。
return adapter.updateOne({ id: action.payload.id, changes: action.payload }, state);
关于angular - NGRX Store adapter.updateone 不更改属性以进行更改检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54558537/