gpt4 book ai didi

angular - @NgRx 实体之间如何建立多对多关系?

转载 作者:行者123 更新时间:2023-12-05 06:16:51 25 4
gpt4 key购买 nike

假设我有这个状态:

{
"appointments": {
"ids": [...],
"entities": [
"0": {
...
mandatoryRecipients: ['0','3']
},
"1": {
...
mandatoryRecipients: ['1','2','0']
}
]
},
"recipients": {
"ids": [...],
"entities": [
"0": {
...
id: '0',
name: 'foo'
},
"1": {
...
id: '1',
name: 'foobar'
},
"2": {
...
id: '2',
name: 'bar'
}
]
}
}

在某个时候,我需要将一个值添加到约会实体的“mandatoryRecipients”数组中。我知道这种状态没有规范化,但我怎样才能规范化它以完成多对多关系?

最佳答案

你需要为此使用一个库。手动覆盖所有内容会花费太多精力。

您可以使用 ngrx-entity-relationship .

比如那个。

const fullAppointment = rootEntity(
selectAppointments, // feature selector, EntityState<Appointment>
relatedEntity(
selectRecipients, // related feature selector, EntityState<Recipient>
'mandatoryRecipients', // key with ids
'mandatoryRecipientsEntities', // key to assign related entities
),
);

const fullAppointments = rootEntities(fullAppointment);

store.select(fullAppointments, ['0', '1']).subscribe(value => {
// value[0].mandatoryRecipientsEntities[0].name works!
});

或者如果你使用 createSelector 那么

const selectFullAppointments = createSelector(
selectIds,
v => v,
(ids, store) => fullAppointments(store, ids);
);

关于angular - @NgRx 实体之间如何建立多对多关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61933118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com