vue代理配置重写方法
在开发中,遇到一个需求,因为后台服务是我自己这边启动的。
所以后台服务地址和tomcat启动的网页地址是一样的,导致我们vue代理的时候,直接跳转到我们tomcat启动的旧页面当中,写入/api便不会跳到tomcat页面,但如此便会导致与接口地址不符
所以需要用路由重写方法来重写。
如下图
rewirte来重写我们请求接口的路由,请求时自动会把/api给去除掉。
vue3重写写法(vite环境下)在vite.config.js中配置
vue3(vue.config.js)
devserver: { proxy: { //代理匹配前缀2 '/api2': { target: 'http://localhost:5001', pathrewrite: {'^/api2': ''}, ws: true, changeorigin: true }, //代理匹配前缀1 '/api': { target: 'http://localhost:5000', pathrewrite: {'^/api': ''}, ws: true,) changeorigin: true } } }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论