Vue路由傳參props解耦的三種方式小結(jié)
路由組件傳參
在組件中使用 $route 會使之與其對應(yīng)路由形成高度耦合,從而使組件只能在某些特定的 URL 上使用,限制了其靈活性。
使用 props 將組件和路由解耦:
布爾模式
商品展示界面?zhèn)鬟fid的動態(tài)參數(shù)
<template>
<div class="home">
<TabBar></TabBar>
<p>這是首頁</p>
<ul>
<li v-for="(item ,index) in goods" :key="item.id">
<!-- <router-link :to="{path:'/detail',query:{item}}"> -->
<router-link :to="{name:'Detail',params:{id:index}}">
<p><img :src="item.path" ></p>
<p>{{item.name}}</p>
<p>{{item.price}}</p>
</router-link>
</li>
</ul>
</div>
</template>
路由配置動態(tài)參數(shù)和開啟props解耦
{
path:'/detail/:id',
name:'Detail',
component:()=> import ('../views/Detail.vue'),
props:true
}
<template>
<div class="detail">
<p>這是詳細(xì)頁</p>
<p>{{id}}</p>
</div>
</template>
<script>
export default{
name:'Detail',
props:['id'],//注意props要和data(){}是同一級的
data(){
return{
}
}
}
</script>
效果:現(xiàn)在點(diǎn)擊了每個商品之后,都能夠?qū)⑾鄳?yīng)的商品id傳遞到詳細(xì)頁面。

注意:對于包含命名視圖的路由,你必須分別為每個命名視圖添加 props 選項(xiàng)
const User = {
props: ['id'],
template: '<div>User {{ id }}</div>'
}
const router = new VueRouter({
routes: [
{ path: '/user/:id', component: User, props: true },
// 對于包含命名視圖的路由,你必須分別為每個命名視圖添加 `props` 選項(xiàng):
{
path: '/user/:id',
components: { default: User, sidebar: Sidebar },
props: { default: true, sidebar: false }
}
]
})
對象模式
{
path:'/detail/:id',
name:'Detail',
component:()=> import ('../views/Detail.vue'),
// props:route=>({params:route.params.id})
props:{id:true}
}
<template>
<div class="detail">
<p>這是詳細(xì)頁</p>
<p>{{id}}</p>
</div>
</template>
<script>
export default{
name:'Detail',
props:['id'],
data(){
return{
}
}
}
</script>

通過這個結(jié)果可以看出,對象模式只能傳布爾值
如果 props 是一個對象,它會被按原樣設(shè)置為組件屬性。當(dāng) props 是靜態(tài)的時(shí)候有用。
const router = new VueRouter({
routes: [
{
path: '/promotion/from-newsletter',
component: Promotion,
props: { newsletterPopup: false }
}
]
})
函數(shù)模式
{
path:'/detail/:id',
name:'Detail',
component:()=> import ('../views/Detail.vue'),
props:route=>({params:route.params.id})//如果使用的是query傳參則改為query
}
<template>
<div class="detail">
<p>這是詳細(xì)頁</p>
<p>{{params}}</p>
</div>
</template>
<script>
export default{
name:'Detail',
props:['params'],
data(){
return{
}
}
}
</script>
效果:

你可以創(chuàng)建一個函數(shù)返回 props。
這樣你便可以將參數(shù)轉(zhuǎn)換成另一種類型,將靜態(tài)值與基于路由的值結(jié)合等等。
const router = new VueRouter({
routes: [
{
path: '/search',
component: SearchUser,
props: route => ({ query: route.query.q })
}
]
})
URL /search?q=vue 會將 {query: 'vue'} 作為屬性傳遞給 SearchUser 組件。
請盡可能保持 props 函數(shù)為無狀態(tài)的,因?yàn)樗粫诼酚砂l(fā)生變化時(shí)起作用。
如果你需要狀態(tài)來定義 props,請使用包裝組件,這樣 Vue 才可以對狀態(tài)變化做出反應(yīng)。
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue項(xiàng)目中keepAlive的使用說明(超級實(shí)用版)
這篇文章主要介紹了Vue項(xiàng)目中keepAlive的使用說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
vue+springboot上傳大文件的實(shí)現(xiàn)示例
本文主要介紹了vue+springboot上傳大文件的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
vue 組件內(nèi)獲取actions的response方式
今天小編就為大家分享一篇vue 組件內(nèi)獲取actions的response方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
vue恢復(fù)初始數(shù)據(jù)this.$data,this.$options.data()解析
這篇文章主要介紹了vue恢復(fù)初始數(shù)據(jù)this.$data,this.$options.data()解析,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Vue3的setup在el-tab中動態(tài)加載組件的方法
公司某項(xiàng)目需求在頁面顯示的組件是根據(jù)角色變化而變化的,怎么實(shí)現(xiàn)這個效果呢,下面小編給大家介紹下Vue3的setup在el-tab中動態(tài)加載組件的方法,需要的朋友可以參考下2022-11-11
vue-cli創(chuàng)建項(xiàng)目時(shí)由esLint校驗(yàn)導(dǎo)致報(bào)錯或警告的問題及解決
這篇文章主要介紹了vue-cli創(chuàng)建項(xiàng)目時(shí)由esLint校驗(yàn)導(dǎo)致報(bào)錯或警告的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
vue3+element-plus動態(tài)路由菜單示例代碼
這篇文章主要介紹了vue3+element-plus動態(tài)路由菜單示例代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-11-11
VsCode新建VueJs項(xiàng)目的詳細(xì)步驟
本篇文章主要介紹了VsCode新建VueJs項(xiàng)目的詳細(xì)步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09

