作用:让路由组件更方便的收到参数。 使用第三种写法时,父路由组件只管传参数,无论query还是params,路由器都能将返回值中所有的key-value以props的形式传给子路由组件,然后子路由组件用props接。
Message.vue
<template>
<div>
<ul>
<li v-for="m in messageList" :key="m.id">
<router-link :to="{
name:'xiangqing',
query:{
id:m.id,
title:m.title
}
}">
{{m.title}}
</router-link>
</li>
</ul>
<hr>
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'Message',
data() {
return {
messageList: [{
id: '001',
title: '消息001'
},
{
id: '002',
title: '消息002'
},
{
id: '003',
title: '消息003'
}
]
}
}
}
</script>
router文件夹下的index.js
import VueRouter from 'vue-router'
import About from '../pages/About'
import Home from '../pages/Home'
import News from '../pages/News'
import Message from '../pages/Message'
import Detail from '../pages/Detail'
export default new VueRouter({
routes: [{
name: 'guanyu',
path: '/about',
component: About
},
{
path: '/home',
component: Home,
children: [{
path: 'news',
component: News,
},
{
path: 'message',
component: Message,
children: [{
name: 'xiangqing',
path: 'detail',
component: Detail,
props($route) {
return {
id: $route.query.id,
title: $route.query.title,
a: 1,
b: 'hello'
}
}
}]
}
]
}
]
})
Detail.vue
<template>
<ul>
<li>消息编号:{{id}}</li>
<li>消息标题:{{title}}</li>
</ul>
</template>
<script>
export default {
name: 'Detail',
props: ['id', 'title']
}
</script>
|