Fork me on GitHub

uni-app写微信小程序,data字段循环引用

在写程序过程中,需要使用到 globalData里的内容,而这个全局变量,在uni-app上需要通过:
var app=getApp();
app.globalData.xxx=xxx来使用。
我觉得每次都要获取app对象,嫌麻烦,就在data数据段里定义一个app字段,
之后就通过 this.app.globalData来使用,问题就出现在这。我用hbuilderX运行到微信小程序开发工具和工具上的真机预览,都没问题,唯独发布后,就报错,相关组件功能失效。报错内容如下:
    --> starting at object with constructor 'vr'
    --- property '_renderProxy' closes the circle>
TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'vr'
    --- property '_renderProxy' closes the circle
    at JSON.stringify ()
    at a.zr [as __patch__] (https://usr/app-service.js:4343:41837)
    at Tn.e._update (https://usr/app-service.js:4343:27516)
看意思知道是循环引用,后来经过反复发布体验版,才知道是问题在这个getApp()的变量不能定义到data数据段里里。
想一想,getApp()获取的是整个app的对象了,每个页面的变量或许都有。那么我在某个页面定义个app,app本来就包含得有这个页面得变量,就形成了循环引用了。
所以以后得注意,这种循环引用,看有没有一些全局字段,相互引用了得。遇到相似问题,也多一种思路。
posted @ 2023-07-27 21:15  HelloLLLLL  阅读(123)  评论(0编辑  收藏  举报