【发布时间】:2021-11-19 10:53:21
【问题描述】:
我将 angular 从 8 更新到 12,在项目的许多地方,控制台中出现错误:TypeError: "property_name" is read-only。
这是一个有错误的对象的例子:
export interface ObjectName {
index: number;
name: string;
widgets: ArrayType[];
}
这是发生错误的代码:
ngOnChanges(): void {
this.Areas = this.areas
.map(area => {
area.index = 10; // ERROR "index" is read-only
switch (area.name) {
case '1': area.index = 0; break;
case '2': area.index = 1; break;
case '3': area.index = 2; break;
}
return area;
})
.sort((a, b) => (a.index - b.index));
}
而且现在项目中有很多这样的地方,我不明白如何修复它。 我尝试将接口更改为一个类,但一切都是一样的。 这是我的 tsconfig.json,但我阅读了它的各种设置,似乎没有什么可以帮助我。
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"downlevelIteration": true,
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"module": "es2020",
"emitDecoratorMetadata": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"importHelpers": true,
"target": "es5",
"typeRoots": ["node_modules/@types"],
"lib": ["es2018", "dom"]
}
}
【问题讨论】:
标签: javascript angular typescript runtime-error typeerror