Vue Router中獲取路由傳遞過(guò)來(lái)的參數(shù)(方法詳解)
在Vue Router中可以通過(guò)動(dòng)態(tài)路由匹配和查詢參數(shù)query來(lái)傳遞參數(shù);同時(shí)也可以將路由參數(shù)或查詢參數(shù)作為組件的props傳遞,這樣組件可以直接通過(guò)props來(lái)訪問(wèn)這些參數(shù)。
1. 動(dòng)態(tài)路由匹配
如果在路由配置中使用了動(dòng)態(tài)路由(如/user/:id),則可以通過(guò)route.params.id獲取該參數(shù)。
// 路由配置
const routes = [
{ path: '/user/:id', component: User }
];
// 組件中獲取參數(shù)
<script setup>
import { useRoute } from 'vue-router';
const route = useRoute();
const userId = route.params.id;
</script>2. 查詢參數(shù)query
如果URL中有查詢字符串(如/user?id=123)或路由跳轉(zhuǎn)時(shí)寫(xiě)了query【如router.push({path: ‘/user’,query: { id: 123}});】,則可以通過(guò)route.query.id獲取該參數(shù)。
// 路由配置
const routes = [
{ path: '/user', component: User }
];
// 組件中獲取參數(shù)
<script setup>
import { useRoute } from 'vue-router';
const route = useRoute();
const userId = route.query.id;
</script>3. 通過(guò) props 傳遞參數(shù)
在路由配置中啟用 props,并將 params 作為 props 傳遞。
// 路由配置
const routes = [
{ path: '/user/:id', component: User, props: true }
];
// 組件中通過(guò) props 獲取參數(shù)
<script setup>
import { defineProps, onMounted } from 'vue';
const props = defineProps({
id: {
type: String,
required: true
}
});
onMounted(() => {
console.log(props.id);
});
</script>通過(guò) props 傳遞查詢參數(shù)。
// 路由配置
const routes = [
{ path: '/user', component: User, props: (route) => ({ id: route.query.id }) }
];
// 組件中通過(guò) props 獲取參數(shù)
<script setup>
import { defineProps, onMounted } from 'vue';
const props = defineProps({
id: {
type: String,
required: true
}
});
onMounted(() => {
console.log(props.id);
});
</script>到此這篇關(guān)于Vue Router中獲取路由傳遞過(guò)來(lái)的參數(shù)(方法詳解)的文章就介紹到這了,更多相關(guān)Vue Router路由參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue?router進(jìn)行路由跳轉(zhuǎn)并攜帶參數(shù)的實(shí)例詳解(params/query)
- Vue中$router.push()路由切換及如何傳參和獲取參數(shù)
- vue-router路由傳參及隱藏參數(shù)問(wèn)題
- vue-router如何實(shí)時(shí)動(dòng)態(tài)替換路由參數(shù)(地址欄參數(shù))
- Vue3使用vue-router如何實(shí)現(xiàn)路由跳轉(zhuǎn)與參數(shù)獲取
- 如何處理vue router 路由傳參刷新頁(yè)面參數(shù)丟失
- vue router動(dòng)態(tài)路由設(shè)置參數(shù)可選問(wèn)題
- vue-router路由參數(shù)刷新消失的問(wèn)題解決方法
- 詳解vue-router2.0動(dòng)態(tài)路由獲取參數(shù)
相關(guān)文章
解決VUE的對(duì)話框el-dialog點(diǎn)擊外部消失問(wèn)題
這篇文章主要介紹了解決VUE的對(duì)話框el-dialog點(diǎn)擊外部消失問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
Vite+Vue3使用MockJS的實(shí)現(xiàn)示例
寫(xiě)一些純前端的項(xiàng)目時(shí),自己造數(shù)據(jù)有些麻煩,于是我們可以利用mock造一些簡(jiǎn)單的數(shù)據(jù),來(lái)滿足我們的需求,本文主要介紹了Vite+Vue3使用MockJS的實(shí)現(xiàn)示例,感興趣的可以了解一下2024-01-01
Vue3中關(guān)于getCurrentInstance的大坑及解決
這篇文章主要介紹了Vue3中關(guān)于getCurrentInstance的大坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
Vue中Key唯一標(biāo)識(shí)作用小結(jié)
key是虛擬DOM的導(dǎo)航儀,通過(guò)唯一標(biāo)識(shí)節(jié)點(diǎn),提高DOM更新效率,不綁定key或使用不穩(wěn)定的key會(huì)導(dǎo)致組件狀態(tài)錯(cuò)亂、性能下降,甚至崩潰,下面就來(lái)介紹一下Vue中Key唯一標(biāo)識(shí)作用,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2026-02-02
解決Vue-cli npm run build生產(chǎn)環(huán)境打包,本地不能打開(kāi)的問(wèn)題
今天小編就為大家分享一篇解決Vue-cli npm run build生產(chǎn)環(huán)境打包,本地不能打開(kāi)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
詳解vuex中的this.$store.dispatch方法
這篇文章主要介紹了vuex中的this.$store.dispatch方法,必須要用commit(‘SET_TOKEN’,?tokenV)調(diào)用mutations里的方法,才能在store存儲(chǔ)成功,需要的朋友可以參考下2022-11-11

