-
在项目的开发之中,做数据请求的时候,会经常遇到跨域的问题。我们可以在
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': ''
}
}
}
......
}
本文介绍了解决项目开发中常见跨域问题的方法。通过在config/index.js文件中配置proxyTable,可以实现对'/api'开头的请求路径进行代理,目标为'http://localhost:4000',并支持跨域及路径重写。
144

被折叠的 条评论
为什么被折叠?



