天天看點

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

一、今天在mpvue開發過程中遇到一個錯誤,隻有每次進入這個有圖表的頁面就會報錯一次,如圖1:

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

                                                                                                                                                         圖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

繼續閱讀