在項目的根目錄創(chuàng)建vue.config.js文件
module.exports = {
devServer: {
open: true, //是否自動彈出瀏覽器頁面
proxy: {
'/api': {
target: 'http://localhost:5000', //API服務(wù)器的地址
ws: true, //代理websockets
changeOrigin: true, // 虛擬的站點需要更管origin
pathRewrite: { //重寫路徑 比如'/api/aaa/ccc'重寫為'/aaa/ccc'
'^/api': ''
}
}
},
}
}
如果有多個需要跨域的服務(wù)器晶府,單獨設(shè)置路徑肯定是最好的桂躏,但如果只向一個服務(wù)器發(fā)送跨域請求,那么簡單點就行川陆。
module.exports = {
devServer: {
proxy: 'http://localhost:4000'
}
}