关于Vue的生产环境proxyTable代理问题

1 .  通过在 config/index.js 配置文件中找到proxyTable配置项
dev: {

    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
      '/api': {  //3
        target: 'http://xxx:8080',
        changeOrigin: true,
        pathRewrite: {
         // 1   '^/api': '/api'    这种接口配置出来     http://xxx:8080/api/getlist
          // 2  ^/api': '/' 这种接口配置出来     http://xxx:8080/getlist
        }
      }
    }
  }

上面红色的1和2的区别

 当你接口有api的时候就需要^api的意思就是有api会首先使用api,防止被系统认为3处的api,如果接口中没有api则不需要,即可以省略,总结:

  接口以“/api”开头的配置 红色1 ,没有则不需要

3 如果配置多个代理

dev: {

    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
      '/api': {  //3
        target: 'http://xxx:8080',
        changeOrigin: true,
        pathRewrite: {
         //   A '^/api': '/api'    这种接口配置出来     http://xxx:8080/api/getlist
          // ^/api': '/' 这种接口配置出来     http://xxx:8080/getlist
        }
      },

   '/api/1': {  //
        target: 'http://xxx:8081',
        changeOrigin: true,
        pathRewrite: {
         //   A '^/api/1': '/api/1'    这种接口配置出来     http://xxx:8081/api/1/getlist
          //   ^/api/1': '/' 这种接口配置出来     http://xxx:80801/getlist
        }
      }
 
    }
  }

上面的调用接口的时候:

 A   /api/1/getlist 即可 http://xxx:8081/api/1/getlist

     /api/getlist 即可 http://xxx:8080/api/getlist

第二种情况

    /api/1/getlist 即可 http://xxx:8081/getlist

     /api/getlist 即可 http://xxx:8080/getlist​​​​​​​

      

猜你喜欢

转载自blog.csdn.net/qq_42306443/article/details/85842600