一、解決方案
1.1 描述接口context-path
后端的兩個(gè)接口服務(wù)請(qǐng)求前綴,如下:
- 前綴1: /bryant
- 前綴2: /
1.2 vue.config.js配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
devServer: { port: 8005, proxy: { // 第一臺(tái)服務(wù)器配置 '/bryant' : { target: 'http://localhost:8081, ws: true, changeOrigin: true, pathRewrite: { ' ^/bryant ': ' /bryant ' } }, // 第二臺(tái)服務(wù)器配置 ' / ': { target: ' http: //localhost:8082', ws: true , changeOrigin: true , pathRewrite: { '^/ ': ' /' } } } } |
1.3 axios修改
1
2
3
4
5
6
7
|
// api base_url,設(shè)置前綴不存在 const BASE_URL = '' // 創(chuàng)建 axios 實(shí)例 const service = axios.create({ baseURL: BASE_URL, timeout: 6000 // 請(qǐng)求超時(shí)時(shí)間 }) |
此時(shí)axios不需要直接指定baseUrl配置
1.4 發(fā)送請(qǐng)求
1
2
3
4
5
6
7
8
9
10
11
12
|
// 請(qǐng)求前綴為“/” this .$http.get( "/basketball" ).then(res => { console.log( '/' , res) }). catch (err => { console.log(err) }) // 請(qǐng)求前綴為“bryant” this .$http.get( "/bryant/mvp" ).then(res => { console.log( '/bryant' , res) }). catch (err => { console.log(err) }) |
總結(jié)
多個(gè)接口服務(wù)的情況下,如果前綴是"/",要將其放在proxy配置的最后一部分,代理的時(shí)候是從上往下查找的,如果放在最上面其他服務(wù)也會(huì)被該配置代理掉
到此這篇關(guān)于詳解vue配置請(qǐng)求多個(gè)服務(wù)端解決方案的文章就介紹到這了,更多相關(guān)vue配置請(qǐng)求多個(gè)服務(wù)端內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://juejin.cn/post/6944937167991308319