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的对象写法 -->
<!-- 采用这种写法path必须采用name的形式 -->
<router-link
:to="{
name: 'xiangqing',
query: {
id: m.id,
title: m.title,
},
}"
>{{ m.title }}</router-link
>
<button @click="pushShow(m)">push查看</button>
<button @click="replaceShow(m)">replace查看</button>
</li>
<hr />
<router-view></router-view>
<!-- <li><a href="/message2">message002</a> </li>
<li><a href="/message/3">message003</a> </li> -->
</ul>
</div>
</template>
<script>
export default {
name: "Message",
data() {
return {
messageList: [
{ id: "001", title: "消息001" },
{ id: "002", title: "消息002" },
{ id: "003", title: "消息003" },
],
};
},
methods: {
//push可以控制页面跳转,replace不储存上一次页面跳转的内容
pushShow(m) {
this.$router.push({
name: "xiangqing",
query: {
id: m.id,
title: m.title,
},
});
},
replaceShow(m) {
this.$router.replace({
name: "xiangqing",
query: {
id: m.id,
title: m.title,
},
});
},
},
};
</script>
<style>
</style>
Banner.vue
<template>
<div class="col-xs-offset-2 col-xs-8">
<div class="page-header">
<h2>Vue Router Demo</h2>
<button @click="back">后退</button>
<button @click="forward">前进</button>
</div>
</div>
</template>
<script>
export default {
name: "Banner",
methods: {
back() {
//back()表示回退上一级
this.$router.back();
},
forward() {
//forward表示回到下一级
this.$router.forward();
},
},
};
</script>
<style>
</style>