根据上一节内容做出更改
Message.vue
<template>
<div>
<ul>
<li v-for="m in messageList" :key="m.id">
<!-- 跳转路由并携带params参数,to的字符串写法 -->
<!-- <router-link :to="`/home/message/detail/${m.id}/${m.title}`">{{m.title}}</router-link> -->
<!-- 跳转路由并携带params参数,to的对象写法 -->
<!-- 特别注意:路由携带params参数时,若使用to的对象写法,则不能使用path配置项,必须使用name配置! -->
<router-link :to="{
name:'xiangqing',
params:{
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>
Detail.vue
<template>
<ul>
<li>消息编号:{{$route.params.id}}</li>
<li>消息标题:{{$route.params.title}}</li>
</ul>
</template>
<script>
export default {
name: 'Detail',
mounted() {
console.log(this.$route)
}
}
</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',
//使用占位符声明接收params参数,这样地址栏也会有这些参数
path: 'detail/:id/:title',
component: Detail,
}]
}
]
}
]
})