vue报错this.**** is not a function的解决方法
前言
本文记录了vue中javascript部分数组使用this.****.forEach((re) => {},
出现报错“this.**** is not a function“,本文提供一种自己使用并有效的解决方法
一.产生原因
代码如下
if (!this.getIsNull(this.transferValue)) {
//let newtransferValue = Object.assign([], this.transferValue)
//console.log(newtransferValue)
console.log(this.transferValue)
this.transferValue.forEach((row) => { }
}
打印结果

导致报错:this.transferValue is not a function
通过打印可以知道,报错是因为原数据是一个__ob__: Observer它不可枚举,从中不可xx[0]取值,虽然不影响程序执行结果,但有错误看着有强迫症
二、解决办法
代码如下:
if (!this.getIsNull(this.transferValue)) {
let newtransferValue = Object.assign([], this.transferValue)
console.log(newtransferValue)
console.log(this.transferValue)
newtransferValue .forEach((row) => { }
}
打印结果

遇到这样的类似错误,如果数据类型没错的话,都可试试将其进一步转化格式深度复制Object.assign([], 数组)或者JSON.parse(JSON.stringify(数组))
总结
有用的话麻烦点个赞,谢谢!!
转载需注明下原创地址哦
本文介绍了在Vue中遇到`this.****.forEach is not a function`错误的原因及解决办法。错误源于尝试对一个Observer对象而非数组调用`forEach`。解决方法是先将数据转换为普通数组,例如使用`Object.assign([], this.transferValue)`或`JSON.parse(JSON.stringify(this.transferValue))`。通过这种方法,可以避免因数据类型导致的错误。
2532

被折叠的 条评论
为什么被折叠?



