尝试分配到只读属性ECMAScript React Native [英] Attempted to assign to readonly property ECMAScript React Native
本文介绍了尝试分配到只读属性ECMAScript React Native的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图为我的组件
中声明的数组赋值。不幸的是,异常被抛出。
TypeError:尝试分配到只读属性
/ pre>
即使我删除
strict
模式,仍然出现异常。可以请人指导我如何使变量既可读又可写?谢谢..!
代码
class RootView extends Component {
cachedData:[]; //声明数组
//尝试在某些函数中分配字典
someFunction(results){
this.cachedData [this.state.searchString.length - 1] =结果;
//这里提出的异常
}
}
解决方案您的语法不正确。将其添加到构造函数中。
class RootView extends Component {
constructor(){
super();
this.cachedData = [];
}
someFunction(results){
this.cachedData [this.state.searchString.length - 1] = results;
}
}
如果您的透析器支持实验代码(第0阶段),您可以使用以下内容:
class RootView extends Component {
cachedData = [];
someFunction(results){
this.cachedData [this.state.searchString.length - 1] = results;
}
}
I am trying to assign a value to an array declared in my
Component
. Unfortunately, exception is thrown.TypeError: Attempted to assign to readonly property
Even if I remove
strict
mode, still exception is being raised. Can please someone guide me how can I make a variable both readable and writable? Thanks..!Code:
class RootView extends Component { cachedData : []; //declared array here //trying to assign dictionary in some function someFunction(results) { this.cachedData[this.state.searchString.length - 1] = results; //exception raised here } }
解决方案Your syntax is incorrect. Add it to a constructor.
class RootView extends Component { constructor() { super(); this.cachedData = []; } someFunction(results) { this.cachedData[this.state.searchString.length - 1] = results; } }
If your transpiler supports experimental code (stage 0), you can use the following:
class RootView extends Component { cachedData = []; someFunction(results) { this.cachedData[this.state.searchString.length - 1] = results; } }
这篇关于尝试分配到只读属性ECMAScript React Native的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文