vue——動態獲取當前url,配置axios的baseURL


參考:https://www.jianshu.com/p/c9324d237a8e

 

關於window.location的詳解:

  window.location 對象不僅可以獲得當前頁面的地址 (URL),還能夠將瀏覽器重定向到新的頁面。

  下面,以http://www.myurl.com:8866/test?id=123&username=xxx為例來進行解釋:

  1. window.location.href (當前url)—— http://www.myurl.com:8866/test?id=123&username=xxx

  2. window.location.protocol(協議)—— http:

  3. window.location.host(域名 + 端口)—— www.myurl.com:8866

  4. window.location.hostname(域名)—— www.myurl.com

  5. window.location.port(端口)—— 8866

  6. window.location.pathname(路徑)—— /test

  7. window.location.search (請求的參數)—— ?id=123&username=xxx

  8. window.location.origin(路徑前面的url)——  http://www.myurl.com:8866

 

main,js中:

···
import Axios from 'axios';

let path =  '/contentPath'
let url = window.location.origin +path; //path為上下文路徑,如果后端有配置就添加
Axios.defaults.baseURL = url;

 


免責聲明!

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



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