在项目的开发之中,做数据请求的时候,会经常遇到跨域的问题。我们可以在
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': ''
}
}
}
......
}
版权声明:本文为weixin_42614080原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。