Vue路由傳參詳細(xì)介紹
路由傳參
效果展示
通過傳參,可以讓Persons路由組建中的內(nèi)容,在新的路由組件Show顯示出來,Show路由組件要嵌套到Persons路由組件中
Persons路組件中的內(nèi)容
params的類型(后附源碼)
path:‘show/:id/:realname’ :id/:realname
,是為傳參所聲明的,props:true 可以理解成可以傳參,這只是其中的一種方法,也是個人覺得最簡單理解的方法。
在persons路由組件中經(jīng)過v-for遍歷數(shù)組獲得值,賦值給傳參目標(biāo)
在show路由組建中,使用props:['id','realname'],這要對應(yīng)上,在index.js所聲明的名字
params的類型源碼不要在意注釋代碼
跟上文順序一樣,一一對應(yīng)
{ 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)">點(diǎn)擊跳轉(zhuǎn)</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路由組件中,需要通過計(jì)算屬性來獲得,傳參的內(nèi)容
query參數(shù)的類型源碼同樣無視注釋代碼
同上順序,一一對應(yīng)
{ 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)">點(diǎn)擊跳轉(zhuǎn)</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
簡化路由的跳轉(zhuǎn) 路由較長的使用名稱
上述query跳轉(zhuǎn) 推薦如下寫法:
<router-link :to="{name:'show',query:{id:item.id,realname:item.realname}}">姓名:{{item.realname}}</router-link> -->
其他代碼與其上相同,如下第二張圖片顯示,起的名字就是這樣的使用方法
到此這篇關(guān)于Vue路由傳參詳細(xì)介紹的文章就介紹到這了,更多相關(guān)Vue路由傳參內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
快速解決vue2+vue-cli3項(xiàng)目ie兼容的問題
這篇文章主要介紹了快速解決vue2+vue-cli3項(xiàng)目ie兼容的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11使用vue-resource進(jìn)行數(shù)據(jù)交互的實(shí)例
下面小編就為大家?guī)硪黄褂胿ue-resource進(jìn)行數(shù)據(jù)交互的實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09VUE搭建分布式醫(yī)療掛號系統(tǒng)后臺管理頁面示例步驟
這篇文章主要為大家介紹了分布式醫(yī)療掛號系統(tǒng)之搭建后臺管理系統(tǒng)頁面,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04解決vue中使用Axios調(diào)用接口時出現(xiàn)的ie數(shù)據(jù)處理問題
今天小編就為大家分享一篇解決vue中使用Axios調(diào)用接口時出現(xiàn)的ie數(shù)據(jù)處理問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08