最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript獲取地址欄參數的方法實現

 更新時間:2023年07月21日 09:52:42   作者:富朝陽  
這篇文章主要給大家介紹了關于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"); // 12

1.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; // abc

2.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>
}

總結

到此這篇關于JavaScript獲取地址欄參數的方法實現的文章就介紹到這了,更多相關JS獲取地址欄參數內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

巴林右旗| 漠河县| 南郑县| 望谟县| 嘉定区| 巴里| 米易县| 丰台区| 天峨县| 阿坝| 安徽省| 靖宇县| 福清市| 五莲县| 江山市| 鲁甸县| 辰溪县| 林西县| 西平县| 平江县| 长宁区| 南澳县| 佛学| 英吉沙县| 恭城| 定州市| 荃湾区| 姜堰市| 精河县| 临朐县| 交城县| 大埔区| 云阳县| 沧源| 正阳县| 大英县| 濮阳市| 南平市| 乃东县| 连州市| 犍为县|