目录
1、创建request全局配置文件
module.exports = (vm) => {
// 初始化请求配置
// 设置跨域问题
// #ifdef H5
// 此为自定义配置参数,具体参数见上方说明
uni.$u.http.setConfig({
baseUrl: 'api',
loadingText: '努力加载中~',
loadingTime: 800,
// ......
});
// #endif
// #ifndef H5
// 此为自定义配置参数,具体参数见上方说明
uni.$u.http.setConfig({
baseUrl: 'http://192.168.xx.xx:xxxxx',
loadingText: '努力加载中~',
loadingTime: 800,
// ......
});
// #endif
}
2、在manifest.json中对H5进行配置
"h5": {
"devServer": {
"proxy": {
"/api": {
"target": "http://192.168.xx.xx:xxxx",
"pathRewrite": {
"^/api": ""//这个api要和request中对H5配置的baseUrl保持一致
}
}
}
}
}
下面示例是文件所在位置
3、在main.js中全局配置
import App from './App'
App.mpType = 'app'
const app = new Vue({
...App
})
// 引入请求封装,将app参数传递到配置中
require('@/config/request.js')(app)
uniapp官网网络请求地址https://uniapp.dcloud.io/api/request/request.html