bladex前端反向代理(解决跨域)

1.vue.config.js

module.exports = {
  //路径前缀
  publicPath: "/",
  lintOnSave: true,
  productionSourceMap: false,
  chainWebpack: (config) => {
    //忽略的打包文件
    config.externals({
      'vue': 'Vue',
      'vue-router': 'VueRouter',
      'vuex': 'Vuex',
      'axios': 'axios',
      'element-ui': 'ELEMENT',
    });
    const entry = config.entry('app');
    entry.add('babel-polyfill').end();
    entry.add('classlist-polyfill').end();
    entry.add('@/mock').end();
  },
  devServer: {
    port: 1888,
    proxy: {
      '/api': {
        //本地服务接口地址
        target: 'http://localhost',
        //远程演示服务地址,可用于直接启动项目
        //target: 'https://saber.bladex.vip/api',
        ws: true,
        pathRewrite: {
          '^/api': '/'
        }
      }
    }
  }
};

对所以请求都加上“api”,反向代理(大概这个意思),实际上去访问的target的url“http://localhost”,同时去掉“api”,这个是80端口,浏览器并不知道这一件事情,

比如:http://localhost:1888/api/blade-subway/test/list?current=1&size=10,这个请求,通过devServer转发,其实访问的http://localhost/blade-subway/test/list?current=1&size=10

原文地址:https://www.cnblogs.com/longsanshi/p/12662098.html