( ⊙ o ⊙ )啊!,
That's impossible!!!
Nothing is impossible!!!
理论上工具HttpManager.js是不可引用 const app = getApp()的,也就是里面不能引用其属性globalData
但可以通过在外部页面,或者app.js里面调用HttpManager.setSomeData(theData)来实现 gloabalData往HttpManager.js传值。
示例:
//app.js
const httpManager = require('utils/HttpManager.js')
App({
onLaunch: function () {
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
httpManager.loginRq({
api: 'login',
data: {code: res.code},
success: function(e) {
let token = e.data.data.token;
httpManager.setGlobalToken(token) //将app内的值传递给HttpManager.js
},
})
}
})
})
//HttpManager.js
const baseUrl = 'http://mydomain/miniprogram/'
var manager = {
setGlobalToken: function(token) {
console.log('tokenSet:' + token)
this.globalToken = token;
},
request: function ({ api, data, method, success, fail, complete }) {
let token = this.globalToken;
if (token == '' ) {
return;
}
wx.request({
url: baseUrl + api,
data: data,
method: method,
success: success,
fail: fail,
complete: complete,
header: {
"content-type": "application/x-www-form-urlencoded",
"x-token": token
}
})
},
..
}
module.exports = {
setGlobalToken: manager.setGlobalToken,
request: manager.request,
loginRq: manager.loginRq
}
或者,用缓存的方式传值。山不过来,我就过去~
网友评论