欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue中如何配置proxy代理

 更新時(shí)間:2023年01月14日 10:01:27   作者:@是靜靜啊  
這篇文章主要介紹了vue中如何配置proxy代理問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

vue配置proxy代理

如果你的前端應(yīng)用和后端 API 服務(wù)器沒有運(yùn)行在同一個(gè)主機(jī)上(即解決跨域問題,用proxy代理請(qǐng)求一下),你需要在開發(fā)環(huán)境下將 API 請(qǐng)求代理到 API 服務(wù)器。

這個(gè)問題可以通過 vue.config.js 中的 devServer.proxy 選項(xiàng)來配置。

轉(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 請(qǐng)求庫(kù) */
import axios from "axios";
/* 組件的配置項(xiàng) */
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 文件中添加如下所示的配置項(xiàng)

module.exports = {
  lintOnSave: false,
  devServer: {
    proxy: "https://apimusic.linweiqin.com/"
  }
};

如果請(qǐng)求有多個(gè)不同的地址

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, // 允許跨域請(qǐng)求
                pathRewrite: {
                    // 重寫路徑,替換請(qǐng)求地址中的指定路徑
                    "^/api": "/", // 將請(qǐng)求地址中的/api替換為空,也就是請(qǐng)求地址中不會(huì)包含/api/
                },
            },
        },
    },
};

關(guān)于/api的詳解

‘/api’:是指遇到這個(gè)字符開頭的話,在這個(gè)字符前面加上target里面的ip或者域名。

舉例:

①登錄接口:http://1.2.3.4:5000/login

…中間省略了配置過程…

②npm run serve:Local: http://localhost:8080/

③點(diǎn)擊后發(fā)送的登錄請(qǐng)求: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

⑥實(shí)際接口當(dāng)中沒有這個(gè)api,所以pathwrite重寫就解決這個(gè)問題的。

pathwrite識(shí)別到api開頭就會(huì)把/api重寫成空,那就是不存在這個(gè)/apil了,完整的路徑又變成:http://1.2.3.4:5000/login

部署因?yàn)?api無法請(qǐng)求到數(shù)據(jù)

接口名稱不用/api,改用實(shí)際接口的第一個(gè)字段,然后取消pathwrite重寫

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論