配置vue.config.js
module.exports = {
devServer: {
// open: true, //是否自動彈出瀏覽器頁面
host: "0.0.0.0",
port: '8080',
https: false,
hotOnly: false,
proxy: {
'/api': {
target: 'http://服務器ip:5000', //API服務器的地址
ws: true, //代理websockets
changeOrigin: true, // 虛擬的站點需要更管origin
pathRewrite: { //重寫路徑 比如'/api/aaa/ccc'重寫爲'/aaa/ccc'
'^/api': ''
}
}
},
}
};
修改main.js
原來
import axios from 'axios'
axios.defaults.baseURL = 'http://服務器ip:5000/'
Vue.prototype.$http = axios
axios.defaults.withCredentials = true; //允許axios請求攜帶cookie等憑證
替換爲
import axios from 'axios'
axios.defaults.baseURL = '/api'
Vue.prototype.$http = axios
axios.defaults.withCredentials = true; //允許axios請求攜帶cookie等憑證
可以看到成功代理