前后端分离_Vue_axios本地跨域(前端localhost:8080到后端localhost:8090)

  1. 找到vue.config.js
module.exports = {
  productionSourceMap: false,
  configureWebpack: {
    devServer: {
      proxy: {
        '/api': {
          target: 'http://localhost:8090', //设置调用的接口域名和端口号(默认端口号80)
          changeOrigin: true, //
          pathRewrite: {
            '^/api': ''
          }
               //这里理解成用‘/api’代替target里面的地址,
              //后面组件中我们掉接口时直接用api代替 
              //比如我要调用'http://40.00.100.100:3002/user/add',
              //直接写‘/api/user/add’即可
        }
      }
    }
  },
  lintOnSave: false
}

  1. 调用实例
  this.$axios
                .get("api/assetPricing/findAll", {
                    params: {},
                })
                .then(res => {
                    // if (res.data.Code == 0) {
                    //     if (res.data.Status == null) {
                    //         this.items = []
                    //     } else {
                    //         this.items = res.data.Status
                    //     }
                    // }
                   window.console.log(res.data);
                })
原文地址:https://www.cnblogs.com/mengxiaoleng/p/13495615.html