JavaScript獲取地址欄參數的方法實現
需求
若地址欄URL為:code-nav/article/917?type=12&title=abc,我們要獲取到地址欄后面的的type和title參數,如何才能拿到呢?
解決方案
1.原生JS實現:
1.1 采用正則表達式獲取地址欄參數(第一種方法)
//獲取地址欄參數,key:參數名稱
function getUrlParams(key) {
let reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
let r = window.location.search.substr(1)
.match(reg);
if (r != null)
return unescape(r[2]);
return null;
}
let title = getUrlParams("title"); // abc
let type = getUrlParams("type"); // 121.2 傳統(tǒng)方法截取實現(第二種方法)
//獲取地址欄參數
function getUrlParams() {
let url = window.location.search; //獲取url中"?"符后的字串
let paramsObj = new Object();
if (url.indexOf("?") != -1) {
let str = url.substr(1);
strs = str.split("&");
for (let i = 0; i < strs.length; i++) {
paramsObj[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
}
}
return paramsObj;
}
let type = getUrlParams().type; // 12
let title = getUrlParams().title; // abc2.Vue框架實現:
2.1 查詢參數獲?。▓鼍耙唬?/h4>
我們需要從地址code-nav/article/917?type=12&title=abc上拿到title的value abc。
<script setup>
import {useRouter} from 'vue-router'
const { currentRoute } = useRouter();
const route = currentRoute.value;
onMounted(()=>{
let type=route.query.type
console.log('type', type) // 12
})
</script>2.2 獲取路徑參數(場景二)
我們需要從地址code-nav/article/917上拿到917這個參數。
首先需要在router/index.js中定義好路由以及路徑參數,如下代碼:
import { createRouter, createWebHistory } from 'vue-router'
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/:id',
name: 'home',
component: () => import('../views/home.vue')
},
]
})
export default router接著就可以在home.vue組件中通過路由useRouter得到參數,注意是route.params,如下代碼:
<script setup>
import {useRouter} from 'vue-router'
const { currentRoute } = useRouter();
const route = currentRoute.value;
onMounted(()=>{
let id=route.params.id
console.log('id', id) // 917
})
</script>3.Angular框架實現:
3.1 矩陣URL參數獲?。▓鼍耙唬?/h4>
參數拼接:
constructor(
private router: Router,
) {}
// 拼裝 matrix url
// code-nav/article;type=12;title=abc
go() {
this.router.navigate(['/code-nav/article', {
type: 12,
title: 'abc',
}]);
}使用 this.route.params 或 this.route.paramMap 來獲取 matrix URL 參數:
constructor(
private route: ActivatedRoute
) { }
ngOnInit() {
// 獲取參數, 使用 params
this.route.params.subscribe(params => {
console.log(params['type'],params['title']);
});
// 使用 paramMap
this.route.paramMap.subscribe(data => {
console.log(data['params'].type,data['params'].title);
})
}3.2 傳統(tǒng)獲?。▓鼍岸?/h4>
snapshot , queryParams , queryParamMap
constructor(
private route: ActivatedRoute
) { }
ngOnInit() {
// 獲取參數, 使用 queryParams
let param1 = this.route.snapshot.queryParams["title"];
let param2 = this.route.snapshot.queryParams["type"];
console.log(param1);
console.log(param2);
this.route.queryParams.subscribe(params => {
console.log(params['title'],params['name']);
});
// 獲取參數, 使用 queryParamMap
this.route.queryParamMap.subscribe(data => {
const params = data['params'];
console.log(params['title'],params['name']);
});
}4.React框架實現:
4.1 查詢參數獲?。▓鼍耙唬?/h4>
import { useParams } from "react-router-dom"
export default function Order() {
let params = useParams()
return <h2>title: {params.title}</h2>
}
import { useParams } from "react-router-dom"
export default function Order() {
let params = useParams()
return <h2>title: {params.title}</h2>
}總結
到此這篇關于JavaScript獲取地址欄參數的方法實現的文章就介紹到這了,更多相關JS獲取地址欄參數內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決火狐瀏覽器下JS setTimeout函數不兼容失效不執(zhí)行的方法
今天檢查自己用JQuery+AJAX+PHP做的網站后臺登錄檢測,愛其他瀏覽器中兼容性還不錯 結果到了火狐(FireFox)瀏覽器下setTimeout這個JS內置函數不執(zhí)行了,本文將提供詳細的解決方法2012-11-11

