vue獲取url上參數(shù)的常見方法小結(jié)
在 Vue 中獲取 URL 參數(shù)有幾種常用方法,具體取決于你的使用場景:
1. 使用 Vue Router(推薦)
查詢參數(shù)(Query Parameters)
// URL: http://example.com/user?id=123&name=john
// 在組件中獲取
export default {
created() {
const id = this.$route.query.id; // "123"
const name = this.$route.query.name; // "john"
console.log(id, name);
}
}路由參數(shù)(Route Params)
// 路由配置
const routes = [
{ path: '/user/:id', component: User }
]
// URL: http://example.com/user/123
export default {
created() {
const id = this.$route.params.id; // "123"
console.log(id);
}
}使用組合式 API(Vue 3)
<template>
<div>用戶ID: {{ userId }}</div>
</template>
<script setup>
import { useRoute } from 'vue-router'
const route = useRoute()
const userId = route.params.id || route.query.id
</script>2. 原生 JavaScript 方法
使用 URLSearchParams
export default {
methods: {
getUrlParams() {
const urlParams = new URLSearchParams(window.location.search)
return {
id: urlParams.get('id'),
name: urlParams.get('name')
}
}
},
created() {
const params = this.getUrlParams()
console.log(params.id, params.name)
}
}傳統(tǒng) URL 解析
export default {
methods: {
getQueryVariable(variable) {
const query = window.location.search.substring(1)
const vars = query.split('&')
for (let i = 0; i < vars.length; i++) {
const pair = vars[i].split('=')
if (pair[0] === variable) {
return decodeURIComponent(pair[1])
}
}
return null
}
},
created() {
const id = this.getQueryVariable('id')
const name = this.getQueryVariable('name')
}
}3. 監(jiān)聽參數(shù)變化
export default {
watch: {
'$route.query': {
handler(newQuery) {
// 查詢參數(shù)變化時(shí)執(zhí)行
console.log('參數(shù)變化:', newQuery)
},
immediate: true // 立即執(zhí)行一次
},
'$route.params': {
handler(newParams) {
// 路由參數(shù)變化時(shí)執(zhí)行
console.log('路由參數(shù)變化:', newParams)
},
immediate: true
}
}
}4. 完整示例
<template>
<div>
<h2>用戶信息</h2>
<p>用戶ID: {{ userId }}</p>
<p>用戶名: {{ userName }}</p>
</div>
</template>
<script>
export default {
data() {
return {
userId: null,
userName: null
}
},
created() {
this.getParams()
},
watch: {
'$route': 'getParams' // 路由變化時(shí)重新獲取參數(shù)
},
methods: {
getParams() {
// 優(yōu)先使用路由參數(shù),其次使用查詢參數(shù)
this.userId = this.$route.params.id || this.$route.query.id
this.userName = this.$route.query.name
console.log('用戶ID:', this.userId)
console.log('用戶名:', this.userName)
}
}
}
</script>使用建議
- ?優(yōu)先使用 Vue Router? - 更加集成和方便
- ?考慮參數(shù)類型轉(zhuǎn)換? - URL 參數(shù)都是字符串,需要時(shí)進(jìn)行類型轉(zhuǎn)換
- ?處理參數(shù)不存在的情況? - 添加適當(dāng)?shù)哪J(rèn)值或錯(cuò)誤處理
- ?監(jiān)聽參數(shù)變化? - 如果需要在同一組件內(nèi)響應(yīng)參數(shù)變化
選擇哪種方法主要取決于你的項(xiàng)目是否使用了 Vue Router 以及具體的業(yè)務(wù)需求。
到此這篇關(guān)于vue獲取url上參數(shù)的常見方法小結(jié)的文章就介紹到這了,更多相關(guān)vue獲取url參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue2.0 watch里面的 deep和immediate用法說明
這篇文章主要介紹了vue2.0 watch里面的 deep和immediate用法說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
Vue3+Element Plus進(jìn)行圖片加載優(yōu)化全攻略
在Web開發(fā)中,未優(yōu)化的圖片會導(dǎo)致很多問題,本文將為大家介紹一下Vue3如何通過Element Plus進(jìn)行圖片加載優(yōu)化,希望對大家有所幫助2025-03-03
詳解關(guān)于vue-area-linkage走過的坑
這篇文章主要介紹了詳解關(guān)于vue-area-linkage走過的坑,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
Vue from-validate 表單驗(yàn)證的示例代碼
本篇文章主要介紹了Vue from-validate 表單驗(yàn)證的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
vue3的介紹和兩種創(chuàng)建方式詳解(cli和vite)
這篇文章主要介紹了vue3的介紹和兩種創(chuàng)建方式(cli和vite),vue3對比vue2帶來的性能提升有很多優(yōu)勢,總體來說Vue 3在性能、開發(fā)體驗(yàn)和代碼組織方面都有所改進(jìn),使得它更加適合于大型、復(fù)雜的應(yīng)用程序開發(fā),需要的朋友可以參考下2023-04-04
淺談vue獲得后臺數(shù)據(jù)無法顯示到table上面的坑
這篇文章主要介紹了淺談vue獲得后臺數(shù)據(jù)無法顯示到table上面的坑,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
vue動態(tài)綁定多個(gè)class以及帶上三元運(yùn)算或其他條件
這篇文章主要介紹了vue動態(tài)綁定多個(gè)class以及帶上三元運(yùn)算或其他條件,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
django+vue項(xiàng)目搭建實(shí)現(xiàn)前后端通信
本文主要介紹了django+vue項(xiàng)目搭建實(shí)現(xiàn)前后端通信,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02

