在manifest.json 的源码视图最下面加上以下代码
"h5": {
"devServer": {
"disableHostCheck": true,
"proxy": {
"/h5api": {
"target": "https://www.abnc.com/api",
"changeOrigin": true,
"secure": false,
"pathRewrite": {
"^/h5api": "/"
}
}
}
}
}
在main.js 封装Request请求并挂载到Vue原型上
Vue.prototype.apiUrl = '/h5api';
Vue.prototype.request = function(obj) {
var header = obj.header || {}
if (uni.getStorageSync('token')) {
header['token'] = uni.getStorageSync("token");
}
uni.request({
url: this.apiUrl + obj.url,
data: obj.data || {},
method: obj.method || 'GET',
header: header,
success: res => {
typeof obj.success == "function" && obj.success(res)
},
fail: res => {
typeof obj.fail == "function" && obj.fail(res)
}
});
}
在页面使用请求
getNotes() {
this.request({
url: '/appNotice/findNoticeBySysCode',
success: res => {
console.log('系统公告', res);
this.noticeList = res.data.list;
}
});
}
最后需要注意的是 在manifest.json 里面的代理地址如果更换了话需要重启项目
重启项目
重启项目