vue 添加代理

1.跟目录下新建vue.config.js文件,添加内容:

module.exports = {
  assetsDir: 'static',
  parallel: false,
  publicPath: './',//history模式注释掉此行,hash模式需要
 
  productionSourceMap: false,
  devServer: {
    port: 8081,
    open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    proxy: {
      '/api': {
              target: 'http://localhost:3000',  //后台接口地址
            changOrigin: true,  //允许跨域
              pathRewrite: {
              /* 重写路径,当我们在浏览器中看到请求的地址为:http://localhost:8080/api/core/getData/userInfo 时
                  实际上访问的地址是:http://121.121.67.254:8185/core/getData/userInfo,因为重写了 /api
                */
                '^/api': ''
              }
            },
    }
  },
}
 
2.把baseUrl改为‘/api’
posted @ 2022-01-14 09:14  闪光123  阅读(95)  评论(0编辑  收藏  举报