vue中如何配置proxy代理
vue配置proxy代理
如果你的前端應(yīng)用和后端 API 服務(wù)器沒有運行在同一個主機上(即解決跨域問題,用proxy代理請求一下),你需要在開發(fā)環(huán)境下將 API 請求代理到 API 服務(wù)器。
這個問題可以通過 vue.config.js 中的 devServer.proxy 選項來配置。
將

轉(zhuǎn)發(fā)到
https://apimusic.linweiqin.com
app.vue文件
<template>
<div id="app">
<h1>hello Vue cli</h1>
<HelloWorld></HelloWorld>
</div>
</template><script>
/* @ => src */
// import HelloWorld from "./components/HelloWorld.vue";
import HelloWorld from "@/components/HelloWorld.vue";
/* 1. 引入 axios 請求庫 */
import axios from "axios";
/* 組件的配置項 */
export default {
created() {
// axios
// .get("song/url?id=504835560")
// .then((res) => {
// console.log(res);
// });
axios
.get("/song/url?id=504835560")
.then((res) => {
console.log(res);
});
axios.get("/api/s/getHotProducts").then(res=>{
console.log(res);
})
},
name: "App",
components: {
HelloWorld
},
};
</script><style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>在 vue.config.js 文件中添加如下所示的配置項
module.exports = {
lintOnSave: false,
devServer: {
proxy: "https://apimusic.linweiqin.com/"
}
};如果請求有多個不同的地址
A: http://s.linweiqin.com/api/s/getHotProducts
B: https://apimusic.linweiqin.com/song/url?id=504835560
module.exports = {
lintOnSave: false,
// devServer: {
// proxy: "https://apimusic.linweiqin.com/"
// }
devServer: {
proxy: {
"/song": {
target: "https://apimusic.linweiqin.com/",
// changeOrigin: true,
},
"/api": {
target: "http://s.linweiqin.com/",
},
},
},
};proxy常用參數(shù)說明
module.exports = {
publicPath: "/",
devServer: {
proxy: {
"/api": {
// 代理名稱 凡是使用/api開頭的地址都是用此代理
target: "http://1.2.3.4:5000/", // 需要代理訪問的api地址
changeOrigin: true, // 允許跨域請求
pathRewrite: {
// 重寫路徑,替換請求地址中的指定路徑
"^/api": "/", // 將請求地址中的/api替換為空,也就是請求地址中不會包含/api/
},
},
},
},
};
關(guān)于/api的詳解
‘/api’:是指遇到這個字符開頭的話,在這個字符前面加上target里面的ip或者域名。
舉例:
①登錄接口:http://1.2.3.4:5000/login
…中間省略了配置過程…
②npm run serve:Local: http://localhost:8080/
③點擊后發(fā)送的登錄請求:http://localhost:8080/api/login
④/api 的作用就是將/api前的localhost:8080變成target的內(nèi)容http://1.2.3.4:5000/
⑤完整的路徑變成了http://1.2.3.4:5000/api/login
⑥實際接口當中沒有這個api,所以pathwrite重寫就解決這個問題的。
⑦pathwrite識別到api開頭就會把/api重寫成空,那就是不存在這個/apil了,完整的路徑又變成:http://1.2.3.4:5000/login

部署因為/api無法請求到數(shù)據(jù)
接口名稱不用/api,改用實際接口的第一個字段,然后取消pathwrite重寫
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue學(xué)習(xí)筆記進階篇之過渡狀態(tài)詳解
本篇文章主要介紹了Vue學(xué)習(xí)筆記進階篇之過渡狀態(tài)詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
vue項目實現(xiàn)會議預(yù)約功能(包含某天的某個時間段和某月的某幾天)
這篇文章主要介紹了vue項目實現(xiàn)會議預(yù)約功能(包含某天的某個時間段和某月的某幾天),本文結(jié)合實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-02-02
基于Vue+element-ui 的Table二次封裝的實現(xiàn)
這篇文章主要介紹了基于Vue+element-ui 的Table二次封裝的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
vue如何移動到指定位置(scrollIntoView)親測避坑
這篇文章主要介紹了vue如何移動到指定位置(scrollIntoView)親測避坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05
element el-table 表格限制多選個數(shù)功能
這篇文章主要介紹了element el-table 表格限制多選個數(shù)功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-03-03

