Ngxs - 是否可以将 Sets 之类的对象添加到商店中?

Ngxs - is adding objects like Sets to the store ok?

是否可以在状态中存储较新的 Javascript 对象,如 Set 或 Map?这会导致奇怪的可变性影响吗?

export interface StateModel {
    aSet: Set<number>;
}

不,这不行,是的,它可以,这取决于你如何使用它。

摘自official docs

Avoid Saving Class Based Instances in Your State

The objects stored in your state should be immutable and should support serialization and deserialization. It is therefore recommended to store pure object literals in your state. Class based instances are not trivial to serialize and deserialize, and also are generally focused on encapsulating internals and mutating internal state through exposed operations. This does not match the requirement for the data stored in state.

This also applies to the usage of data collections such as Set, Map, WeakMap, WeakSet, etc. Since they are not amenable to deserialization and cannot easily be presented for normalization.