如下图,例如在user.js页面
我通过app.globalData来获得全局变量,其中有text和open
但始终open 无法输出出来是什么地方的问题呢
为什么我在其他页面通过globalData方式,获得的openid为undefined ?Why do I obtain undefined openid through globalData on other pages?
为什么我在其他页面通过globalData方式,获得的openid为undefined ?如下图,例如在user.js页面
我通过app.globalData来获得全局变量,其中有text和open
但始终open 无法输出出来是什么地方的问题呢
As shown in the following figure, for example, in the user.js Page I pass app.globalData To get the global variables, where there are text and open, but open can't be output all the time. What's the problem
回答:
楼主遇到的是时序问题,建议阅读开发文档 Api Promise
提示的内容意思大概是"打印的内容只是当前这个值的内容,不一定是打印时这个值的内容"
你试试console.log(JSON.parse(JSON.stringify(app.globalData)))
这样打印出来值的内容,可以保证是你打印时的值的内容
通俗一点就是因为你open是异步赋值的 你同步console.log的时候此时open还没有赋值