问题描述
我有一个Angular 10(Typescript 3.9)项目,具有以下类别:
export class Document {
constructor(
public id: number,...
public tags: Tag[]
) {
this.id = id;
...
this.tags = tags;
}
}
如果我尝试更改标签(重新分配或推送),
document.tags = ...
例如在一个现有对象中,我得到:
只读
我希望这种行为。您是否知道此错误来自何处?
我最近从Angular 7升级到了10,在此之前一切正常,但是升级说明并未提及这种行为。
停用严格模式(即使被认为是不好的做法)也没有用。
你有什么主意吗?
解决方法
这是评论的答案。
更改ngrx存储的对象可能会导致这种行为,因为存储区会保护从reducer外部进行更改的对象。
在Angular 8之前,大多数人还安装了getServerSideProps
以恢复安全性。借助Angular 8,该功能已内置。
您可以尝试通过将ngrx-store-freeze
设置为strictStateImmutability
来禁用它,但是不建议这样做,因为存储现在可以包含不可预测的更改。
false
,
首先保留文档,请不要使用它。并查看下面的示例
class Tst {
constructor(
public id: number,public tags: number[]
) {
this.id = id;
this.tags = tags;
}
}
var x = new Tst(1,[1,2])
x.id = 12
x.tags = [3,4]