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ù)變化時執(zhí)行
console.log('參數(shù)變化:', newQuery)
},
immediate: true // 立即執(zhí)行一次
},
'$route.params': {
handler(newParams) {
// 路由參數(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ù)
},
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ù)都是字符串,需要時進行類型轉(zhuǎn)換
- ?處理參數(shù)不存在的情況? - 添加適當?shù)哪J值或錯誤處理
- ?監(jiān)聽參數(shù)變化? - 如果需要在同一組件內(nèi)響應(yīng)參數(shù)變化
選擇哪種方法主要取決于你的項目是否使用了 Vue Router 以及具體的業(yè)務(wù)需求。
到此這篇關(guān)于vue獲取url上參數(shù)的常見方法小結(jié)的文章就介紹到這了,更多相關(guān)vue獲取url參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue實現(xiàn)將網(wǎng)頁內(nèi)容轉(zhuǎn)換為圖片并保存到本地
在?Vue2?項目中,將網(wǎng)頁內(nèi)容轉(zhuǎn)換為圖片并保存到本地,常??梢酝ㄟ^第三方庫實現(xiàn),本文為大家整理了常用方案,實現(xiàn)步驟及示例代碼,需要的可以了解下2025-05-05
vue 函數(shù)調(diào)用加括號與不加括號的區(qū)別
這篇文章主要介紹了vue 函數(shù)調(diào)用加括號與不加括號的區(qū)別,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下2020-10-10
element-ui?tree?手動展開功能實現(xiàn)(異步樹也可以)
這篇文章主要介紹了element-ui?tree?手動進行展開(異步樹也可以),項目中用到了vue的element-ui框架,用到了el-tree組件,需要的朋友可以參考下2022-08-08
Element-UI組件實現(xiàn)面包屑導(dǎo)航欄的示例代碼
面包屑導(dǎo)航欄是一種用戶界面組件,用于展示用戶在網(wǎng)站或應(yīng)用中的路徑,它包括了從主頁到當前頁面的鏈接序列,有助于用戶快速了解和導(dǎo)航至上級頁面,本文就來介紹一下Element-UI組件實現(xiàn)面包屑導(dǎo)航欄的示例代碼,感興趣的可以了解一下2024-09-09

