目的:在配置文件config/index.js中配置統一請求接口
在配置文件config/index.js找到
module.exports={
dev:{
proxyTable:{}
}
}
更改為:
proxyTable:{ "/api": { target: "http://218.78.187.216/api/v1", //設置調用的接口域名和端口 changeOrigin: true, pathRewrite: { "^/api": "" //用'/api' 代替 'http://218.78.187.216/api/v1' } } }
例如:請求地址為:"http://218.78.187.216/api/v1/authentication"
請求的時候可以寫為:
this.$axios.post("/api/anthentication",params).then(res=>{ console.log(res); },err=>{ console.log(err); })
proxyTable: { "/api": { target: "http://www.liulongbin.top:3005/api", //設置調用的接口域名和端口 changeOrigin: true, //是否跨域 pathRewrite: { "^/api": "" //用'/api' 代替 'http://www.liulongbin.top:3005/api' } } },
- 使用axios調用示例:
getlunbo() { let _this = this; _this.$axios.get('/api/getlunbo').then(function(result){ console.log(result) }) }
在瀏覽器中看到的訪問地址:
其實真正訪問的地址是: http://www.liulongbin.top:3005/api/getlunbo
轉自:https://blog.csdn.net/XuMiao_/article/details/88653606
https://blog.csdn.net/qq_36111804/article/details/82878158