声明:本站文章均为作者个人原创,图片均为实际截图。如有需要请收藏网站,禁止转载,谢谢配合!!!

1、common.js定义两个公共函数

function toast(title = '', icon = 'none') {
    uni.showToast({
        title: title,
        icon: icon
    });
}
function myRequest(url = "", data = {}, method = 'POST'){
    return new Promise((res,rej)=>{
        uni.request({
            url: this.$apiUrl + url,
            method: method,
            header: {
                "Xshop-Token": uni.getStorageSync("zhsgToken")
            },
            data: data,
            success(data) {
                res(data)
            },
            fail() {
                rej()
            }
        })
    })
}
export {toast, myRequest}

2、main.js引入并挂载到原型上

import {toast, myRequest} from './common.js'

Vue.prototype.$toast = toast
Vue.prototype.$myRequest = myRequest

3、index.js中使用公共函数

this.$toast('test')
this.$myRequest('/api/test', {'id': 1})