如果我在打字稿中有以下界面
interface Member {
readonly id: number;
readonly name: string;
readonly email: string;
groups: <ReadonlyArray>Group
}
interface Group {
readonly id: number;
readonly name: string;
readonly active: boolean;
}
数据看起来像这样
member {
id: 1,
name: 'John Doe',
email: 'junk@junk.com'
groups: [
{ id: 1, name: 'Group 1', active: true}
{ id: 2, name: 'Group 2', active: false}
{ id: 3, name: 'Group 3', active: false}
]
}
如果我想将它存储在 akita/state 中并允许它是可变的(即,也许用户想要单击一个复选框来表示“Group 2”现在处于活动状态并且我需要更新 akita/state),我该怎么做整个对象的副本,因此它不是不可变的(在插入秋田/状态之前)?
好的,我想在阅读以下评论后编辑我的问题。
我将以下代码插入我的秋田商店
loadMemberById(memberId: number) {
const request$ = this.membersDataService.getById(memberId).pipe(
tap(response => this.membersStore.upsert(response.id, response)),
tap(response => this.membersStore.upsert(response.id, { groups: {
...response.groups } })),
tap(response => this.membersStore.setHasCache(true))
);
return cacheable(this.membersStore, request$);
}
VS 在抱怨state.member.groups
里面arrayUpdate
update(groupId: number, value: boolean): Observable<void> {
const observer = this.groupsDataService.update(groupId, value);
observer.subscribe(() => {
this.sessionStore.update(state => {
return {
member: {
...state.member,
groups: arrayUpdate(state.member.groups, data => data.groupId ===
groupId, {
active: value
})
}
};
});
});
谢谢jonpfl