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(数组))

总结

有用的话麻烦点个赞,谢谢!!
转载需注明下原创地址哦

Logo

基于 Vue 的企业级 UI 组件库和中后台系统解决方案,为数万开发者服务。

更多推荐