首先建立common.js文件
const status = 1
//
const REQUEST = function(_url, _data, _getWay, _cb) {
let that = this
uni.request({
url: _url,
data: _data || '',
method: _getWay,
header: {
'content-type': 'application/json',
// 'token': app.globalData.token,
},
success: function(res) {
if (res.data.code == 2) {
that.status = 0
}
return typeof _cb === 'function' && _cb(res.data);
},
})
}
export default {
REQUEST,
status
}
然后到main.js引入common.js文件
import common from './static/common/common.js'
//挂载
Vue.prototype.GLOBAL = common;
这样需要的页面就能直接使用而不需要每次都引入
console.log(this.GLOBAL.status)
全局变量第二种做法的话只需要在App.vue定义globalData就OK了。跟小程序一样
export default {
globalData: {
text: 'text'
}
}