1,新建一个request文件
const baseUrl = '/api/'
const request = (url = '', date = {}, type = '', header = {
}) => {
return new Promise((resolve, reject) => {
uni.showLoading({
title: '加载中',
})
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
2,在main.js里引用
import Vue from 'vue'
import App from './App'
import { myRequest } from './utils/api.js'
import request from './common/request.js'
Vue.config.productionTip = false;
//挂载到全局,让所有页面都能接收
Vue.prototype.$myRequest = myRequest; //挂载到Vue的原型上
Vue.prototype.$request = request
App.mpType = 'app';
const app = new Vue({
...App
})
app.$mount()
3,在页面中运用
getList(index) {
this.$request('/api/eurekamedicalsystem/v2/medical-emrquality/seconditem', {
// 传参参数名:参数值,如果没有,就不需要传
"wardCode": index,
// "key": this.searchValue
},'get').then(res => {
// 打印调用成功回调
console.log(res)
this.datalist = res;
})
},
网友评论