vue使用代理实现开发阶段跨域

时间:2023-03-09 15:31:14
vue使用代理实现开发阶段跨域

在config/index.js找到 proxyTable对象,添加键值对即可。

"/api":{

  target:"http://192.168.1.1",

  changeOrighn:true,

  pathRewrite:{

    "^/api":" "

  }

}

倘若加上了这对键值对,意思就是以后的请求路径中存在 /api ,就是将 /api 隐式替换成 target+pathRewrite["/api"]

可能说了这样说,大家还是有点不明白,那就举一个例子:

  在使用vue时,请求地址,均为 localhost开头,若需要请求到 192.168.1.115/dataForYou 接口的数据,那么就存在跨域限制,那么就可以在配置文件中加上

"/api":{

  target:"http://192.168.1.115",

  changeOrighn:true,

  pathRewrite:{

    "^/api":" "

  }

},然后直接请求 /api/dataForYou,即可获得数据,并且其中 /api 可以保存为全局变量。

那么里面的pathRewrite到底有什么作用呢?我想应该是为了方便接口,

假如,有大量接口 {192.168.1.15/a/1-100}{100}, {198.168.1.15/b/1-100}{100};那么定义target为192.168.1.15,在pathRewrite中定义