angular - 角度类属性到常量变量
问题描述
我已经为常量变量分配了一个类属性。常量是可变的,当我改变常量时,类属性也在改变。
export class SampleClass implements OnInit {
name = "a";
onclick() {
const tempName = this.name;
tempName = "b"; // error
console.log(this.name); // a
}
}
[编辑]和我上面问的一样的场景,
export class AppComponent implements OnInit {
name = [
{"option":"Never", "weight":0},
{"option":"Sometimes", "weight":1},
{"option":"Many time", "weight":2},
{"option":"Most of the time", "weight":3},
{"option":"All the time", "weight":4}
];
ngOnInit() {
const tempName = this.name;
tempName[0].weight = 10; // constant is changing
console.log(this.name); // this.name[0].weight also set to 10
console.log(tempName);
}
}
如何取消链接?
解决方案
推荐阅读
- ms-access - MS Access 中条件的 Like 子句中的参数
- c++ - 在 C++ 中统一使用后台线程进行图像处理会导致崩溃
- ios - 将 JSON 解码为可编码对象 - 有条件
- r - R中的二阶多元多项式回归
- python - 无法让我的脚本执行下一页按钮上的所有点击
- stanford-nlp - Stanford-CoreNLP:解析长句时的 requests.exceptions.HTTPError
- java - 将图像转换为按钮数组
- jquery - 单击父选项卡时如何默认打开子选项卡
- c# - 带有布尔值的 if 语句进入 switch 方法
- ios - 在 iOS 中更改用户密码时,MSGraphSDK 用户详细信息 API 回调没有响应