Vue路由傳參詳細介紹
路由傳參
效果展示
通過傳參,可以讓Persons路由組建中的內容,在新的路由組件Show顯示出來,Show路由組件要嵌套到Persons路由組件中
Persons路組件中的內容
params的類型(后附源碼)
path:‘show/:id/:realname’ :id/:realname
,是為傳參所聲明的,props:true 可以理解成可以傳參,這只是其中的一種方法,也是個人覺得最簡單理解的方法。
在persons路由組件中經過v-for遍歷數(shù)組獲得值,賦值給傳參目標
在show路由組建中,使用props:['id','realname'],這要對應上,在index.js所聲明的名字
params的類型源碼不要在意注釋代碼
跟上文順序一樣,一一對應
{ path:'/persons', component:Persons, children:[ { path:'show/:id/:realname',component:Show,props:true // name:'show', path:'show',component:Show } ] },
<template> <div> <ul > <li v-for="item in persons" :key="item.id"> <router-link :to="`/persons/show/${item.id}/${item.realname}`">姓名:{{item.realname}}</router-link> <!-- <router-link :to="`/persons/show/?id=${item.id}&realname=${item.realname}`">姓名:{{item.realname}}</router-link> --> <!-- <router-link :to="{name:'show',query:{id:item.id,realname:item.realname}}">姓名:{{item.realname}}</router-link> --> <button @click="push(item)">點擊跳轉</button> </li> </ul> <hr> <router-view></router-view> </div> </template> <script> export default { name:'Persons', data(){ return{ persons:[ {id:1,realname:'張三'}, {id:2,realname:'李四'}, {id:3,realname:'王五'}, {id:4,realname:'趙六'} ] } }, methods: { // push(item){ // this.$router.push(`/persons/show/${item.id}/${item.realname}`) // }, }, } </script> <style> </style>
<template> <div> id:{{id}}姓名:{{realname}} </div> </template> <script> export default { name:'Show', props:['id','realname'], data(){ return{ } }, computed: { // id(){ // return this.$route.query.id // }, // realname(){ // return this.$route.query.realname // } }, } </script> <style> </style>
??????query參數(shù)的類型
跟普通的路由聲明一樣,這里起的名字,在后續(xù)會使用到
在persons路由組件的遍歷,得到想要的值,與上面一種方法有著很大的區(qū)別
<router-link :to="`/persons/show/?id=${item.id}&realname=${item.realname}`">姓名:{{item.realname}}</router-link>
?id=${item.id}&realname=${item.realname}
是獲取id,獲取姓名
在show路由組件中,需要通過計算屬性來獲得,傳參的內容
query參數(shù)的類型源碼同樣無視注釋代碼
同上順序,一一對應
{ path:'/persons', component:Persons, children:[ { // path:'show/:id/:realname',component:Show,props:true name:'show', path:'show',component:Show } ] },
<template> <div> <ul > <li v-for="item in persons" :key="item.id"> <!-- <router-link :to="`/persons/show/${item.id}/${item.realname}`">姓名:{{item.realname}}</router-link> --> <router-link :to="`/persons/show/?id=${item.id}&realname=${item.realname}`">姓名:{{item.realname}}</router-link> <!-- <router-link :to="{name:'show',query:{id:item.id,realname:item.realname}}">姓名:{{item.realname}}</router-link> --> <!-- <button @click="push(item)">點擊跳轉</button> --> </li> </ul> <hr> <router-view></router-view> </div> </template> <script> export default { name:'Persons', data(){ return{ persons:[ {id:1,realname:'張三'}, {id:2,realname:'李四'}, {id:3,realname:'王五'}, {id:4,realname:'趙六'} ] } }, methods: { // push(item){ // this.$router.push(`/persons/show/${item.id}/${item.realname}`) // }, }, } </script> <style> </style>
<template> <div> id:{{id}}姓名:{{realname}} </div> </template> <script> export default { name:'Show', // props:['id','realname'], data(){ return{ } }, computed: { id(){ return this.$route.query.id }, realname(){ return this.$route.query.realname } }, } </script> <style> </style>
路由name
簡化路由的跳轉 路由較長的使用名稱
上述query跳轉 推薦如下寫法:
<router-link :to="{name:'show',query:{id:item.id,realname:item.realname}}">姓名:{{item.realname}}</router-link> -->
其他代碼與其上相同,如下第二張圖片顯示,起的名字就是這樣的使用方法
到此這篇關于Vue路由傳參詳細介紹的文章就介紹到這了,更多相關Vue路由傳參內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
VUE搭建分布式醫(yī)療掛號系統(tǒng)后臺管理頁面示例步驟
這篇文章主要為大家介紹了分布式醫(yī)療掛號系統(tǒng)之搭建后臺管理系統(tǒng)頁面,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04解決vue中使用Axios調用接口時出現(xiàn)的ie數(shù)據處理問題
今天小編就為大家分享一篇解決vue中使用Axios調用接口時出現(xiàn)的ie數(shù)據處理問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08