编程式路由导航

作用

不借助router-link实现路由跳转,让路由跳转更加灵活

使用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//$routere的两个API
this.$router.push({
name:'xiangqing',
params:{
id:xxx,
title:xxx
}
})

this.$router.replace({
name:'xiangqing',
params:{
id:xxx,
title:xxx
}
})

this.$router.forward() //前进
this.$router.back() //后退
this.$router.go(3) //可前进也可后退

案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<template>
<div>
<ul>
<li v-for="message in messages" :key="message.id" >

<!-- <router-link :to="`/home/message/detail/${message.id}/${message.title}`">{{message.title}}</router-link>&nbsp;&nbsp;-->

<router-link :to="{name:'xiangqing',query:{
id:message.id,
title:message.title
}}">{{message.title}}</router-link>&nbsp;&nbsp;

<button @click="pushShow(message)">push查看</button>
<button @click="replaceShow(message)">replace查看</button>
</li>
</ul>

<router-view></router-view>
</div>
</template>


<script>
export default {
name: "Message",
data(){
return{
messages:[{id:"001",title:"消息001"},
{id:"002",title:"消息002"},
{id:"003",title:"消息003"}
]

}
},
methods:{
pushShow(message){
this.$router.push({name:'xiangqing',query:{
id:message.id,
title:message.title
}}

)
},
replaceShow(message){
this.$router.replace(
{name:'xiangqing',query:{
id:message.id,
title:message.title
}}
)
}
}
}
</script>


<style scoped>

</style>