query参数
不打扰路由配置
传递(携带)参数
<router-link :to="`/home/message/detail?id=${m.id}&title=${m.title}`">{{m.title}}</router-link>
<router-link
:to="{
path:'/home/message/detail',
query:{
id:m.id,
title:m.title
}
}">
{{m.title}}
</router-link>
接收(读取)参数
$route.query.id
$route.query.title
命名路由
路由切换路径过长时,简化路由跳转
给路由命名:
export default new VueRouter({
routes:[
{
path:'/about',
component:About
},
{
path:'/home',
component:Home,
children:[
{
path:'news',
component:News
},
{
path:'message',
component:Message,
children:[
{
name:'detail',
path:'detail',
component:Detail
},
]
}
]
}
]
})
简化跳转(对象写法下才可以使用):
<router-link
:to="{
path:'/home/message/detail',
query:{
id:m.id,
title:m.title
}
}">
{{m.title}}
</router-link>
<router-link
:to="{
name:'detail',
query:{
id:m.id,
title:m.title
}
}">
{{m.title}}
</router-link>
params参数
配置路由,声明接收params参数:
children:[
{
name:'detail',
path:'detail/:id/:title',
component:Detail
},
]
传递params参数:
<router-link :to="`/home/message/detail/${m.id}/${m.title}`">{{m.title}}</router-link>
<router-link
:to="{
name:'detail',
params:{
id:m.id,
title:m.title
}
}">
{{m.title}}
</router-link>
注意:路由携带params参数时,对象写法必须使用name配置项,不能使用path配置项
接收(读取)参数
$route.params.id
$route.params.title
路由的props配置
为了让路由组件在接收参数时更方便,只需要在props 中声明,不需要再重复写繁琐的计算属性
{
path:'message',
component:Message,
children:[
{
name:'detail',
path:'detail/:id/:title',
component:Detail,
props($route){
return {id:$route.params.id,title:$route.params.title}
}
},
]
}
Detail组件中接收(读取)参数
<li>消息id:{{id}}</li>
<li>消息内容:{{title}}</li>
props:['id','title']
|