typeof cb == "function" && cb(obj)
但凡用了Promise,這種方式就可以拋棄了.
Page({
data: {},
onLoad() {
request('https://api.it120.cc/jy02149522/banner/list', {
type: 0
}, function(obj) {
console.log('請求到的數據:', obj)
})
}
})
// request請求封裝
const request = (url, data, cb) => (
wx.request({
url,
data,
method: 'GET',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
dataType: 'json',
responseType: 'text',
success(res) {
//判斷cb是不是function,並且執行這行這個function
res.data.code == 0 ? typeof cb == "function" && cb(res.data) : typeof error === 'function' && error()
},
fail() {
typeof error === 'function' && error()
},
complete(res) {
// res.data.code != 0 ? typeof cb == "function" && cb(res.data) : typeof error === 'function' && error()
}
})
)
各位看官,自個理會.
