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