vue3 proxy本地代理

第一步在vue.config.js写上代理

devServer: {
    hot: true,
    compress: true,
    disableHostCheck: true,
    proxy: {
      "/api": {
        target: "http://xxxx:8002/Home",//你的接口地址
        changeOrigin: true,//是否跨域
        pathRewrite: {
          "^/api": ""//接口重写,表示/api开头的就是代理到target,即是http://xxxx:8002/Home
        }
      }
    }
  },

第二步,在axios文件里面配置baseurl

const instance3 = axios.create({
  baseURL: "/api", //这里会读取vue.config里面的配置。自动重写地址。
  timeout: 60000,
  headers: {
    "Content-Type": "application/json; charset=utf-8"
  }
});

第三部,接口请求

 var _this = this;
      _this
        .$axios3({
          method: "post",
          url: "gettask", //正常请求接口地址就可以了
          data: { id:1 }
        })
        .then(function (res) {
          console.log(res);
        });

vue3 proxy本地代理_第1张图片
这就是完整的本地代理设置

你可能感兴趣的:(vue3 proxy本地代理)