vue項目config/index.js中proxyTable用法


目的:在配置文件config/index.js中配置統一請求接口

在配置文件config/index.js找到

 module.exports={
        dev:{
            proxyTable:{}
        }
 }

更改為:

 proxyTable:{
        "/api": {
                target: "http://218.78.187.216/api/v1", //設置調用的接口域名和端口
                changeOrigin: true,
                pathRewrite: {
                      "^/api": "" //用'/api' 代替 'http://218.78.187.216/api/v1'
                }
              }    
    }

例如:請求地址為:"http://218.78.187.216/api/v1/authentication"
請求的時候可以寫為:

    this.$axios.post("/api/anthentication",params).then(res=>{
        console.log(res);
    },err=>{
        console.log(err);
    })
proxyTable: {
 "/api": {
    target: "http://www.liulongbin.top:3005/api", //設置調用的接口域名和端口
    changeOrigin: true,    //是否跨域
    pathRewrite: {
      "^/api": "" //用'/api' 代替 'http://www.liulongbin.top:3005/api'
    }
  }
},
  1. 使用axios調用示例:
getlunbo() {
    let _this = this;
    _this.$axios.get('/api/getlunbo').then(function(result){
        console.log(result)
    })
}

在瀏覽器中看到的訪問地址:
瀏覽器中看到的訪問地址

 

 

 

其實真正訪問的地址是: http://www.liulongbin.top:3005/api/getlunbo

轉自:https://blog.csdn.net/XuMiao_/article/details/88653606

https://blog.csdn.net/qq_36111804/article/details/82878158


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM