vue-router 路由組件傳值 解耦 props


在組件中使用$route會使之與其對應路由形成高度耦合,使用props將組件和路由解耦:

routes:[

{

path:'/user',

component:User,

children:[

{path:info/:id,props:true,component:Info},//boolean值     

{path:info1,props:{name:'zhangsan'},component:Info},//靜態對象    通過$attrs獲取props中的數據
{path:info2/:id,props:()=>{name:'zhangsan3333'},component:Info}//函數   通過$attrs獲取props中的數據   動態路由參數依然使用route.params.id
]

}

]
</script>
Info.vue
<template>

<div>{{id}}</div>//可以直接獲取動態路由參數

</template>

<script>

export default{

props:{

id:{type:String,default:''}

}

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM