Vue3路由的props案例詳解
Vue3路由的props
props的作用
它在路由里面的作用很簡(jiǎn)單,就是是把路由參數(shù)、或者自定義數(shù)據(jù),直接喂給路由組件當(dāng)作props使用,上面說(shuō)的路由參數(shù)也就是我們之前使用params或者query傳的參數(shù);
props的寫法
- 布爾值
只傳遞params參數(shù),參數(shù)名和組件的props一致,寫法如:{ path: ‘/goods/:goodsId’, component: GoodsDetail, props: true }
- 對(duì)象
傳遞自定義的靜態(tài)數(shù)據(jù),與路由參數(shù)毫無(wú)關(guān)系,寫法如:{ path: ‘/goods/:goodsId’, component: GoodsDetail, props: { isHot: true } }
- 函數(shù)
傳遞params/query參數(shù),轉(zhuǎn)換參數(shù)類型,拼接自定義數(shù)據(jù),這個(gè)一般情況下是最常用的;寫法如:{ path: ‘/goods/:goodsId’, component: GoodsDetail, props: (route) => ({ goodsId: Number(route.params.goodsId), source: route.query.source }) }
小案例
比如我們以一個(gè)非常簡(jiǎn)單的校園社團(tuán)系統(tǒng)作為例子
- 首先我們先看路由配置
//引入vue-router相關(guān)函數(shù),創(chuàng)建路由實(shí)例,并使用HTML5的history模式
import { createRouter, createWebHistory } from "vue-router";
import ClubDetailBool from "../components/ClubDetailBool.vue";
import ClubDetailFunc from "../components/ClubDetailFunc.vue";
import ClubDetailObj from "../components/ClubDetailObj.vue";
const routes = [{
path: '/club/bool/:clubId',
name: 'ClubDetailBool',
component: ClubDetailBool,
props: true
}]
const router = createRouter({
history: createWebHistory(),
routes
});
export default routes;- 現(xiàn)在我們先創(chuàng)建一個(gè)社團(tuán)列表的組件
<template>
<div class="club-list">
<h2>校園社團(tuán)列表</h2>
<button @click="goToBool(1)">查看【布爾值寫法】籃球社詳情</button>
</div>
</template>
<script>
import { useRouter } from 'vue-router';
const router = useRouter();
const goToBool = (clubId) => {
router.push(`/club/bool/${clubId}`);
};
</script>
<style scoped>
button {
margin: 10px;
padding: 8px 16px;
cursor: pointer;
}
</style>- 我們先說(shuō)第一個(gè)布爾值的案例
//ClubDetailBool.vue
<template>
<div class="club-detail">
<h3>社團(tuán)詳情</h3>
<!-- 這個(gè)只能拿到params里面的clubID,拿不到靜態(tài)數(shù)據(jù)和query參數(shù) -->
<p>社團(tuán)ID:{{ clubId }}</p>
</div>
</template>
<script setup>
//接受props名稱必須和路由參數(shù)名稱一致
const props = defineProps({
clubId: {
type: String,
required: true
}
});
</script>
- 對(duì)象和函數(shù)的案例
import { createRouter, createWebHistory, RouteLocationNormalized } from 'vue-router'
// 導(dǎo)入三個(gè)組件,分別對(duì)應(yīng)三種props寫法(實(shí)際項(xiàng)目可復(fù)用一個(gè)組件,這里為了清晰分開(kāi))
import ClubDetailBool from '../components/ClubDetailBool.vue'
import ClubDetailObj from '../components/ClubDetailObj.vue'
import ClubDetailFunc from '../components/ClubDetailFunc.vue'
const routes = [
// 寫法1:布爾值 —— 只傳遞params參數(shù)(clubId),參數(shù)名和組件props名一致
{
path: '/club/bool/:clubId',
name: 'ClubDetailBool',
component: ClubDetailBool,
props: true // 布爾值寫法:自動(dòng)把params里的clubId傳給組件的props
},
// 寫法2:對(duì)象 —— 傳遞靜態(tài)數(shù)據(jù)(和路由參數(shù)無(wú)關(guān))
{
path: '/club/obj/:clubId', // 路徑仍帶clubId,但對(duì)象寫法拿不到,僅做演示
name: 'ClubDetailObj',
component: ClubDetailObj,
props: {
recruitStatus: '招新中', // 靜態(tài)數(shù)據(jù):不管路由參數(shù)是什么,組件都能拿到這個(gè)值
maxMember: 50 // 靜態(tài)數(shù)據(jù):社團(tuán)最大人數(shù)50
}
},
// 寫法3:函數(shù)(最常用)—— 混合傳遞params/query/靜態(tài)數(shù)據(jù),還能處理參數(shù)類型
{
path: '/club/func/:clubId',
name: 'ClubDetailFunc',
component: ClubDetailFunc,
props: (route: RouteLocationNormalized) => ({
clubId: Number(route.params.clubId), // 把params的clubId(字符串)轉(zhuǎn)數(shù)字
source: route.query.source || '首頁(yè)', // 拿query參數(shù)(比如/club/func/1?source=社團(tuán)列表)
recruitStatus: '招新中', // 靜態(tài)數(shù)據(jù)
maxMember: 50 // 靜態(tài)數(shù)據(jù)
})
}
]
const router = createRouter({
history: createWebHistory(),
routes
})
export default router- 創(chuàng)建對(duì)象案例的組件
//ClubDetailObj.vue
<template>
<div class="club-detail">
<h3>【對(duì)象寫法】社團(tuán)詳情</h3>
<p>招新?tīng)顟B(tài):{{ recruitStatus }}</p>
<p>最大人數(shù):{{ maxMember }}</p>
<p>備注:只能拿到靜態(tài)數(shù)據(jù),拿不到clubId/query參數(shù)</p>
</div>
</template>
<script setup>
// 接收props:名稱和路由props對(duì)象里的鍵一致
const props = defineProps({
recruitStatus: String,
maxMember: Number
})
</script>- 父組件
<template>
<div class="club-list">
<h2>校園社團(tuán)列表</h2>
<!-- 跳轉(zhuǎn)布爾值寫法的詳情頁(yè) -->
<button @click="goToBool(1)">查看【布爾值寫法】籃球社詳情</button>
<!-- 跳轉(zhuǎn)對(duì)象寫法的詳情頁(yè) -->
<button @click="goToObj(1)">查看【對(duì)象寫法】籃球社詳情</button>
<!-- 跳轉(zhuǎn)函數(shù)寫法的詳情頁(yè)(帶query參數(shù)) -->
<button @click="goToFunc(1)">查看【函數(shù)寫法】籃球社詳情</button>
</div>
</template>
<script setup>
import { useRouter } from 'vue-router'
const router = useRouter()
// 跳轉(zhuǎn)布爾值寫法
const goToBool = (clubId) => {
router.push({
name: 'ClubDetailBool',
params: { clubId }
})
}
// 跳轉(zhuǎn)對(duì)象寫法
const goToObj = (clubId) => {
router.push({
name: 'ClubDetailObj',
params: { clubId } // 雖然傳了clubId,但對(duì)象寫法拿不到
})
}
// 跳轉(zhuǎn)函數(shù)寫法(帶query參數(shù))
const goToFunc = (clubId) => {
router.push({
name: 'ClubDetailFunc',
params: { clubId },
query: { source: '社團(tuán)列表' }
})
}
</script>
<style scoped>
button {
margin: 10px;
padding: 8px 16px;
cursor: pointer;
}
</style>- App.vue也需要樣式,讓其好看一點(diǎn)
<template>
<!-- 頁(yè)面整體布局 -->
<div id="app">
<!-- 導(dǎo)航欄:快速切換不同路由(三種 props 寫法) -->
<nav class="nav">
<h2>Vue3 路由 props 三種寫法演示</h2>
<div class="nav-buttons">
<!-- 跳轉(zhuǎn)布爾值寫法路由 -->
<router-link to="/club/bool/1" class="nav-link" active-class="active">
布爾值寫法(僅傳 params)
</router-link>
<!-- 跳轉(zhuǎn)對(duì)象寫法路由 -->
<router-link to="/club/obj/1" class="nav-link" active-class="active">
對(duì)象寫法(僅傳靜態(tài)數(shù)據(jù))
</router-link>
<!-- 跳轉(zhuǎn)函數(shù)寫法路由(帶 query 參數(shù)) -->
<router-link to="/club/func/1?source=導(dǎo)航欄" class="nav-link" active-class="active">
函數(shù)寫法(混合傳參+類型處理)
</router-link>
<!-- 回到社團(tuán)列表頁(yè) -->
<router-link to="/" class="nav-link" active-class="active">
社團(tuán)列表(跳轉(zhuǎn)測(cè)試)
</router-link>
</div>
</nav>
<!-- 路由出口:所有路由組件都會(huì)渲染到這里 -->
<main class="content">
<router-view />
</main>
</div>
</template>
<script setup>
// App.vue 作為根組件,這里無(wú)需額外邏輯,只需要提供路由出口即可
// 若需要全局樣式/全局邏輯,可在這里寫
</script>
<style scoped>
/* 全局樣式(scoped 僅作用于 App.vue,也可抽離到全局樣式文件) */
#app {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
font-family: Arial, Helvetica, sans-serif;
}
/* 導(dǎo)航欄樣式 */
.nav {
padding: 20px;
background-color: #f5f5f5;
border-radius: 8px;
margin-bottom: 20px;
}
.nav h2 {
margin: 0 0 15px 0;
color: #333;
}
.nav-buttons {
display: flex;
gap: 10px;
flex-wrap: wrap;
}
/* 路由鏈接樣式 */
.nav-link {
padding: 8px 16px;
text-decoration: none;
color: #42b983;
/* Vue 主題色 */
border: 1px solid #42b983;
border-radius: 4px;
transition: all 0.2s;
}
.nav-link:hover {
background-color: #42b983;
color: white;
}
/* 激活的路由鏈接樣式 */
.nav-link.active {
background-color: #42b983;
color: white;
font-weight: bold;
}
/* 內(nèi)容區(qū)域樣式 */
.content {
padding: 20px;
border: 1px solid #eee;
border-radius: 8px;
}
</style>


到此這篇關(guān)于Vue3路由的props的文章就介紹到這了,更多相關(guān)Vue3路由props內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VUE使用localstorage和sessionstorage實(shí)現(xiàn)登錄示例詳解
這篇文章主要為大家介紹了VUE使用localstorage和sessionstorage實(shí)現(xiàn)登錄示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
vue.js父子組件通信動(dòng)態(tài)綁定的實(shí)例
今天小編就為大家分享一篇vue.js父子組件通信動(dòng)態(tài)綁定的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue 彈窗時(shí) 監(jiān)聽(tīng)手機(jī)返回鍵關(guān)閉彈窗功能(頁(yè)面不跳轉(zhuǎn))
這篇文章主要介紹了vue 彈窗時(shí) 監(jiān)聽(tīng)手機(jī)返回鍵關(guān)閉彈窗功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值(頁(yè)面不跳轉(zhuǎn)) ,需要的朋友可以參考下2019-05-05
Vue使用<Suspense/>實(shí)現(xiàn)圖片加載組件
Suspense是Vue的內(nèi)置組件,用于管理異步組件的加載狀態(tài),包括等待、出錯(cuò)和最終渲染,它通過(guò)兩個(gè)插槽(default和fallback)來(lái)實(shí)現(xiàn)這一功能,感興趣的可以了解一下2026-01-01
vue實(shí)現(xiàn)高德地圖添加多個(gè)點(diǎn)標(biāo)記
地圖多點(diǎn)標(biāo)注其實(shí)是個(gè)非常簡(jiǎn)單的問(wèn)題,這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)高德地圖添加多個(gè)點(diǎn)標(biāo)記的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
vue實(shí)現(xiàn)簡(jiǎn)單的購(gòu)物車功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡(jiǎn)單的購(gòu)物車功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
Vue+EleMentUI實(shí)現(xiàn)el-table-colum表格select下拉框可編輯功能實(shí)例
這篇文章主要給大家介紹了關(guān)于Vue+EleMentUI實(shí)現(xiàn)el-table-colum表格select下拉框可編輯功能的相關(guān)資料,element-UI表格的使用相信大家都不陌生,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2023-07-07
Vue 監(jiān)聽(tīng)元素前后變化值實(shí)例
這篇文章主要介紹了Vue 監(jiān)聽(tīng)元素前后變化值實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07

