在config index.js文件中
引入如下代码即可
const path = require('path')
const devEnv = require('./dev.env')
module.exports = {
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: devEnv.OPEN_PROXY === false ? {} : {
'/api': {
target: 'http://localhost:8083',
changeOrigin: true,
pathRewrite: {
'^/api': '/'
}
}
},
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: devEnv.OPEN_PROXY === false ? {} : {
'/api': {
target: 'http://localhost:8083', //需要更改的后端的端口号
changeOrigin: true,
pathRewrite: {
'^/api': '/'
}
}
},
这里的配置是正则表达式,以/api开头的将会被用用‘/api'替换掉,假如后台文档的接口是 /api/list/xxx
//前端api接口写:axios.get('/api/list/xxx') , 被处理之后实际访问的是:http://news.baidu.com/api/list/xxx
}
}},