[Vue warn]: Property or method “toJSON“ is not defined on the instance but referenced during render.

一、今天在mpvue开发过程中遇到一个错误,只有每次进入这个有图表的页面就会报错一次,如图1:

 

                                                                                                                                                         图1

二、完整描述如下:

[Vue warn]: Property or method "toJSON" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

(found in pages/projects/hotal/index.vue)

这段报错大概是说toJSON这个方法我们没有定义就使用了,可是仔细发现页面中并没有用到,于是我去查了报错的解决办法,发现网上很多类似的情况,有的说是:

  • “这是微信开发者工具的bug,请回退微信开发者工具到2020年6月那个版本或之前版本(截止2020.11.10未修复),见文档:http://doc.ucharts.cn/1074674。我的也又再现此问题,不理他了。另外确实在vue的export default里的methods里增加toJSON(){},就解决问题。”
  • “因为实例混乱引起的 把ucharts实例定义在全局 不要定义在vue里面就行”

三、解决办法如下:

  1. 下载旧版微信开发工具,1.03.2006090
  2. 自定义toJSON(){}方法,注意:自定义空的toJSON方法但是有副作用,容易把vue 中data的数据搞丢了
  3. ucharts对象定义到data外边,不要把对象定义再Vue 的data中去
  4. 修改ucharts代码,在外面调用createCanvasContext,传给ucharts,而不传this

四、参考文章:

[1] uni-app 使用ucharts时,报错 Property or method "toJSON" is not defined

扫描二维码关注公众号,回复: 12997102 查看本文章

[2] Property or method "toJSON"

猜你喜欢

转载自blog.csdn.net/XU441520/article/details/115177511