溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Vue怎么獲取url路由地址和參數(shù)

發(fā)布時間:2023-03-31 10:54:53 來源:億速云 閱讀:164 作者:iii 欄目:開發(fā)技術(shù)

這篇“Vue怎么獲取url路由地址和參數(shù)”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Vue怎么獲取url路由地址和參數(shù)”文章吧。

1.window.location

實例:http://www.myurl.com:8866/test?id=123&username=xxx

當(dāng)前URL

window.location.href
結(jié)果:http://www.myurl.com:8866/test?id=123&username=xxx

協(xié)議

window.location.protocol
結(jié)果:http

域名 + 端口

window.location.host
結(jié)果:www.myurl.com:8866

域名

window.location.hostname()
結(jié)果:www.myurl.com

端口

window.location.port()
結(jié)果:8866

路徑部分

window.location.pathname()
結(jié)果:/test

請求的參數(shù)

window.location.search
結(jié)果:?id=123&username=xxx

備注:獲取參數(shù)

// var url="www.baidu.com?a=1&b=2&C=3";//測試地址

/*
 * 分析:最前面是?或&,緊跟著除 ?&#以外的字符若干
 * 然后再等號,最后再跟著除 ?&#以外的字符
 * 并且要分組捕獲到【除?&#以外的字符】
 */
 
var reg=/[?&]([^?&#]+)=([^?&#]+)/g;
var param={};
var ret =  reg.exec(url);
while(ret){ // 當(dāng)ret為null時表示已經(jīng)匹配到最后了,直接跳出
	param[ret[1]]=ret[2];
	ret = reg.exec(url);
}
console.log(param)

獲取’?'前邊的URL

window.location.origin()
結(jié)果:http://www.myurl.com:8866

獲取#之后的內(nèi)容

window.location.hash
結(jié)果:null

2.vue-router 獲取參數(shù)

  • this.$route

  • this.$route.fullPath

  • this.$route.hash

  • this.$route.matched

  • this.$route.meta

  • this.$route.params

  • this.$route.query

補充:vue獲取地址欄地址url截取參數(shù)

vue獲取地址欄地址url截取參數(shù) 方法 (新建js文件)

export function UrlSearch(){<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->
let name,value,str=location.href,num=str.indexOf("?"); //取得整個地址欄
str=str.substr(num+1); //取得所有參數(shù) stringvar.substr(start [, length ]
let arr=str.split("&"); //各個參數(shù)放到數(shù)組里
console.log(arr)
for(let i=0;i < arr.length;i++){<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->
num=arr[i].indexOf("=");
if(num>0){<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->
name=arr[i].substring(0,num);
value=arr[i].substr(num+1);
this[name]=value;
}
}
}

在main.js引入

Vue怎么獲取url路由地址和參數(shù)

掛載到全局

Vue怎么獲取url路由地址和參數(shù)

使用

Vue怎么獲取url路由地址和參數(shù)

以上就是關(guān)于“Vue怎么獲取url路由地址和參數(shù)”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI