-
在項目的開發之中,做數據請求的時候,會經常遇到跨域的問題。我們可以在
config
目錄下的index.js
文件中,配置proxyTable
去解決。 -
/api
是匹配所有以'/api'
開頭的請求路徑,target
是代理目標的基礎路徑,changeOrigin
是支持跨域,pathRewrite
是重寫路徑,去掉路徑中開頭的'/api'
,代碼如下所示:
const path = require('path')
module.exports = {
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
'/api': {
target: 'http://localhost:4000',
changeOrigin: true,
pathRewrite: {
'^/api': ''
}
}
}
......
}