详解vue配置请求多个服务端解决方案
作者:王、先生 时间:2024-05-05 09:06:11
一、解决方案
1.1 描述接口context-path
后端的两个接口服务请求前缀,如下:
前缀1: /bryant
前缀2: /
1.2 vue.config.js配置
devServer: {
port: 8005,
proxy: {
// 第一台服务器配置
'/bryant': {
target: 'http://localhost:8081,
ws: true,
changeOrigin: true,
pathRewrite: {
'^/bryant': '/bryant'
}
},
// 第二台服务器配置
'/': {
target: 'http://localhost:8082',
ws: true,
changeOrigin: true,
pathRewrite: {
'^/': '/'
}
}
}
}
1.3 axios修改
// api base_url,设置前缀不存在
const BASE_URL = ''
// 创建 axios 实例
const service = axios.create({
baseURL: BASE_URL,
timeout: 6000 // 请求超时时间
})
此时axios不需要直接指定baseUrl配置
1.4 发送请求
// 请求前缀为“/”
this.$http.get("/basketball").then(res => {
console.log('/', res)
}).catch(err => {
console.log(err)
})
// 请求前缀为“bryant”
this.$http.get("/bryant/mvp").then(res => {
console.log('/bryant', res)
}).catch(err => {
console.log(err)
})
来源:https://juejin.cn/post/6944937167991308319
标签:vue,配置,服务端
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JavaScript Dom编程:介绍学习书籍
2008-02-20 08:32:00
基于Python手写拼音识别
2022-10-22 09:24:09
![](https://img.aspxhome.com/file/2023/4/69574_0s.png)
直接生成XML的Google SiteMap的asp代码
2007-08-17 13:44:00
GO语言(golang)基础知识
2024-02-13 18:04:03
Docker安装常用组件(mysql,redis)的方法
2024-01-28 02:44:56
Oracle开发之分析函数简介Over用法
2024-01-17 08:40:45
Python3将数据保存为txt文件的方法
2023-01-22 19:06:27
![](https://img.aspxhome.com/file/2023/0/75330_0s.png)
浅析DW4中的站点管理
2007-02-03 11:40:00
![](https://img.aspxhome.com/file/uploadpic/20072/20072311403769.gif)
JavaScript文档生成工具
2007-10-26 11:59:00
Python 实现劳拉游戏的实例代码(四连环、重力四子棋)
2022-09-16 00:07:30
![](https://img.aspxhome.com/file/2023/2/110412_0s.png)
教你如何用python开发一款数字推盘小游戏
2021-11-03 23:22:14
![](https://img.aspxhome.com/file/2023/4/71404_0s.png)
Node Express用法详解【安装、使用、路由、中间件、模板引擎等】
2024-05-11 10:49:24
30个最常用css选择器解析
2011-06-16 20:36:37
python实现中文文本分句的例子
2023-02-15 12:16:24
如何在Frontpage中定义CSS样式
2008-08-02 12:32:00
python 名称空间与作用域详情
2021-06-10 23:12:38
![](https://img.aspxhome.com/file/2023/8/71548_0s.png)
Vue.2.0.5过渡效果使用技巧
2024-04-10 10:33:12
![](https://img.aspxhome.com/file/2023/4/139764_0s.png)
SQLServer2005 中的几个统计技巧
2024-01-18 00:28:01
![](https://img.aspxhome.com/file/2023/4/102064_0s.jpg)
在SQL Server中使用CLR调用.NET方法
2008-12-24 15:43:00
python Scrapy爬虫框架的使用
2023-07-16 01:03:09