在app.js使用全局变量
// 在app.js文件中
this.globalData = {
identity: 2,
}
// 在需要使用全局变量的页面的js文件中
const app = getApp()
********
data: {
identity: app.globalData.identity,
},
*******
onLoad: function (options) {
this.identity = app.globalData.identity
}
就可以在小程序中使用全局变量啦!