vue项目如何配置proxyTable解决跨域问题

  1. 在项目的开发之中,做数据请求的时候,会经常遇到跨域的问题。我们可以在config目录下的index.js文件中,配置proxyTable去解决。

  2. /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': ''
        }
      }
    }
    ......
}
发布了146 篇原创文章 · 获赞 34 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_42614080/article/details/104057329