postGet.js
//请求接口的域名
const host = 'http://localhost/mpvue/php/'
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)
}
var aa=11
export default {
aa,
request,
get,
post,
host
}
app的main.js
import WXrequest from './datas/postGet'
// 将$httpWX对象放置在vue的原型上为的是每个实列都可以用
Vue.prototype.$httpWX = WXrequest
需要使用的页面
beforeMount() {
this.fun();
},
methods: {
fun() {
console.log(1)
this.$httpWX.get({
//请求接口
url: 'wenbeng.php',
data: {
}
}).then(res => {
console.log(res)
this.listData =res;
})
},
}