const host=""//如果有则输入 function request(url,method,data,header={}){wx.showLoading({title:'正在加载中....'})return new Promise((resolve,reject)=>{wx.request({url:url,method:method,data:data,header:{'content-type':'application/json'},success:function(res){wx.hideLoading();resolve(res.data)},fail:function(res){wx.hideLoading();reject(res);},complete:function(res){wx.hideLoading();}})}) }//封装get方法 function get(obj){return request(obj.url,'GET',obj.data) } //封装post方法 function post(obj){return request(obj.url,'POST',obj.data) }
(二)在main.js中引入原型(最外层的main.js,用作全局变量)
import HttpPost from "./utils/httppost';//引入 Vue.prototype.$wxhttp=HttpPost;//作为全局变量