我們都知道,前端開發跨域是一個很常見的問題,當然跨域的方法也有很多,現在我就給大家分享一個在vue項目中如何使用webpack做代理,步驟簡單,操作方便,本人親測,巨好使😆
首先,找到你的config文件夾,打開index.js
找到dev下面的 proxyTable
proxyTable: {
'/api': {
target: 'http://172.00.61.243:8082', // 你接口的域名 http://172.00.61.243:8082
//secure: false, // 如果是https接口,需要配置這個參數
changeOrigin: true, // 如果接口跨域,需要進行這個參數配置
pathRewrite: {
'^/api': ''
}
}
},
接着去你的index.html入口文件里面設置代理頭
const IS_DEBUG = true; //如果是測試環境就是true,如果是生產環境就是false
const commonUrl = IS_DEBUG ? '' : 'http://172.00.61.243:8082';
之后就可以去頁面中做請求了
this.$http.get(commonUrl + '/api/getLocations').then(response => {
// console.log(response);
}, response => {
alert(response);
});
分享完畢😆