webpack之proxyTable设置跨域

使用vue-cli搭建的vue项目可以使用在项目内设置代理(proxyTable)的方式来解决跨域问题。

dev:{
// 静态资源文件夹
    assetsSubDirectory: 'static',

    // 发布路径
    assetsPublicPath: '/',

    // 代理配置表,在这里可以配置特定的请求代理到对应的API接口
    // 例如将'localhost:8080/api/xxx'代理到'www.example.com/api/xxx'
    // 使用方法:https://vuejs-templates.github.io/webpack/proxy.html
proxyTable: {
      '/api': {
        target: 'http://www.ykt.com/',//接口域名
        changeOrigin: true,//是否跨域
        pathRewrite: {
          '^/api': '/index/index'//需要rewrite重写
        }
      }
    }

实现效果:

localhost:8080/api/getbuildcate   ===>  www.ykt.com/index/index/getbuildcate

参考:https://blog.csdn.net/u013575984/article/details/78319528

参考:https://www.cnblogs.com/webhmy/p/9340361.html

参考:https://www.cnblogs.com/wancheng7/p/8987694.html

参考:https://blog.csdn.net/zhushikezhang/article/details/79962468

原文地址:https://www.cnblogs.com/jvziking/p/11721159.html