使用webpack配置vue项目代理 (超简单)

  我们都知道,前端开发跨域是一个很常见的问题,当然跨域的方法也有很多,现在我就给大家分享一个在vue项目中如何使用webpack做代理,步骤简单,操作方便,本人亲测,巨好使😆

  首先,找到你的config文件夹,打开index.js

  找到dev下面的 proxyTable 

 

    proxyTable: {

    '/api': {

            target: 'http://172.00.61.243:8082', // 你接口的域名  http://172.00.61.243:8082

            //secure: false,      // 如果是https接口,需要配置这个参数

            changeOrigin: true,     // 如果接口跨域,需要进行这个参数配置

            pathRewrite: {

              '^/api': ''

            }

        }

   },

  

  接着去你的index.html入口文件里面设置代理头  

 

  const IS_DEBUG = true; //如果是测试环境就是true,如果是生产环境就是false

const commonUrl = IS_DEBUG ? '' : 'http://172.00.61.243:8082';

 

  之后就可以去页面中做请求了

  

  this.$http.get(commonUrl + '/api/getLocations').then(response => {

// console.log(response);

}, response => {

alert(response);

});

 

  分享完毕😆

posted @ 2017-11-21 16:28  波吉大王子  阅读(5478)  评论(0编辑  收藏  举报