1.在common目录下的config.js文件中写线上环境(上面那个api接口前缀,下面那个是websocket地址)
2.在common目录下新建一个request.js方法
代码如下:
import $C from '@/common/config.js';
export default {
common:{
method: 'GET',
header:{
"content-type":"application/json"
},
data:{}
},
request(options = {}){
options.url = $C.webUrl + options.url
options.method = options.method || this.common.method
options.header = options.header || this.common.header
return uni.request(options);
},
get(url,data = {},options = {}){
options.url = url
options.data = data
options.method = 'GET'
return this.request(options)
},
post(url,data = {},options = {}){
options.url = url
options.data = data
options.method = 'POST'
return this.request(options)
},
}
3.在main.js中挂载
4.使用封装的请求
方式一:
方式二: