美文网首页
uni封装一个request

uni封装一个request

作者: 上海_前端_求内推 | 来源:发表于2021-09-24 09:17 被阅读0次

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;
                })
            },

相关文章

网友评论

      本文标题:uni封装一个request

      本文链接:https://www.haomeiwen.com/subject/pinagltx.html