proxy 跨域配置, 针对有axios的baseURL

1.首先主要的config文件下的index.js中的proxytable配置
proxyTable:{

  '/proxy': {

    target:'http://192.168.2.141:8080', 

    changeOrigin:true,

    pathRewrite: {
      '^/proxy': ''
    }
  }
}
//proxy这个词可以任意换,但文下的词要一致

2.config下的dev.env.js中配置base_api,即与上文对应的proxy

module.exports = merge(prodEnv, {
  NODE_ENV: '"development"',
  BASE_API: '"/proxy"'
})

//这里是开发模式下的配置,产品模式到prod.env.js中配置

3.axios中的全局设置配置

axios.defaults.baseURL = process.env.BASE_API;

4.请求的时候,例如我现在需要请求 http://192.168.2.141:8080/WX/shopCart/findShopCartList 这个接口,这样写就行了 ↓

getData(){
        this.axios({
          method:'post',
          url:'/WX/shopCart/findShopCartList',
          data:{
           
          }

        }).then(res=>{
            console.log(res,'返回的数据')
        }

好了大功告成!

你可能感兴趣的:(javascript,vue.js,跨域,axios)