vue axios使用,vue-router獲取url傳參
獲取(qu)url參(can)數有兩種情況:
應用場景一:內部頁面之間互相傳值
假如要從A頁面跳轉到B頁面,
this.$router.push({path:"/B",query:{ Id : this.tId , ... }})
進(jin)入B頁(ye)面之(zhi)后,
this.Id = this.$route.query.Id;
就(jiu)能將A頁面的id傳入B頁面,進行參數獲取。
應用場景二:外系統跳轉Vue項目時自帶參數
例如:
//shq5785.blog.csdn.net/list?Id=sunhuaqiang1#/
外(wai)系統跳轉進(jin)入的url會在Vue項(xiang)目編(bian)譯(yi)后(hou)自動(dong)添加(jia) “#/”后(hou)綴 ,其中帶的參(can)數在 “#/”之(zhi)前,所以應(ying)用(yong)場景(jing)一(yi)中的方(fang)法(fa)無法(fa)獲(huo)取(qu),只能應(ying)用(yong)JS的方(fang)法(fa)來獲(huo)取(qu),解析功能函數如下所示(shi):
getURLParams(url) { let param = url.split('#')[0]; //獲取#/之前的字符串 var paramContent = param.split('?')[1]; //獲取?之后的參數字符串 var paramsArray = paramContent.split('&'); //參數字符串分割為數組 var paramResult = {}; //遍歷數組,拿到json對象 paramsArray.forEach((item, index, paramsArray) => { paramResult[paramsArray[index].split('=')[0]] = paramsArray[index].split('=')[1]; }) return paramResult; }
調(diao)用方式(shi)如下(xia):
this.getURLParams(window.location.href)
vue-router獲取url傳(chuan)參 | 《Linux就該這么學》 (linuxprobe.com)