使用 ngrx 或 ngxs 时,我们会自动获得对象不变性吗?

Do we get object immutability automatically when using ngrx or ngxs?

除了 ngrx 之外,在使用 NGRX or NGXS? Most of the contexts that I read up on seem to indicate this, but there's also wording like"When we use NGRX with immutable objects ...", so just want to double check that we are not suppose to use something like immutable.js 时,我们是否会自动获得所有域实例的对象不可变性?

不,我们没有。如果您不使用不可变库,则在添加到状态之前,必须在 reducer (NGRX) 或 action handler (NGXS) 中手动深度克隆传递的有效负载。

NGXS 在开发模式下使用不可变对象。请参考 ngxs/store 存储库中的 state-operations.ts 文件来验证这一点。

从第 36 行开始,代码如下:

if (this._config.developmentMode) {
  return this.ensureStateAndActionsAreImmutable(rootStateOperations);
}

ensureStateAndActionsAreImmutable 中有 deepFreeze() 对值执行 Object.freeze(value)

因此在使用 ngxs 时不需要不可变、immer 或任何不可变库。

如果您使用 NGRX,您可能希望使用 ngrx-store-freeze instead of third party libs. This has one dependency(deep-freeze-strict) 以确保不变性。