在common 新建request.js
const baseUrl = 'XXXXXXXXXXX'
const request = (url = '', type = '',date = {}, header = {
}) => {
return new Promise((resolve, reject) => {
uni.request({
method: type,
url: baseUrl + url,
data: date,
header: header,
dataType: 'json',
}).then((response) => {
setTimeout(function() {
uni.hideLoading();
}, 200);
let [error, res] = response;
resolve(res.data);
}).catch(error => {
let [err, res] = error;
reject(err)
})
});
}
export default request
在main.js 当中
import request from '@/common/service/request.js'
Vue.prototype.$request = request
在具体页面当中
this.$request('/sys/mLogin', 'POST',{
// 传参参数名:参数值,如果没有,就不需要传
username:this.userName,
password:this.password
}
).then(res => {
// 打印调用成功回调
console.log(res)
})