1.创建src下创建utils/wx-request.js
const host = 'http://10.0.0.6:8081' function request (url, method, data, header = {}) {
wx.showLoading({
title: '加载中' // 数据请求前loading
})
return new Promise((resolve, reject) => {
wx.request({
url: host + url, // 仅为示例,并非真实的接口地址
method: method,
data: data,
headers: {
'content-type': 'application/json' // 默认值
},
success: function (res) {
wx.hideLoading()
resolve(res.data)
},
fail: function (res) {
wx.hideLoading()
// reject(false)
},
complete: function () {
wx.hideLoading()
}
})
})
} function get (obj) {
return request(obj.url, 'GET', obj.data)
} function post (obj) {
return request(obj.url, 'POST', obj.data)
} export default {
request,
get,
post,
host
}
2.main.js中引入到原型
import Vue from 'vue'
import App from './App'
import WXrequest from './utils/wx-request' Vue.prototype.$httpWX = WXrequest
3.使用
this.$httpWX.post({
url: '/data.json',
data: { }
}).then(res => {
console.log(res)
})