实现效果:
功能类似于浏览器的前进和后退按钮
关键代码:
<button @click="$router.go(1)">前进</button>
<button @click="$router.go(-1)">后退</button>
完整代码:
在此基础上(Vue 点击按钮替换页面_Zhichao_97的博客-CSDN博客)修改ToOtherPage.vue的代码:
<template>
<div>
<h1>ToOtherPage</h1>
<button @click="ToOtherPage">去首页</button>
<!-- 替换页面-->
<button @click="replacePage">替换页面</button>
<button @click="$router.go(1)">前进</button>
<button @click="$router.go(-1)">后退</button>
</div>
</template>
<script>
export default {
name: "ToOtherPage",
methods:{
ToOtherPage(){
// this.$router.push("/")
// this.$router.push({path:'/'})
// 袖带参数跳转
// this.$router.push({path:"/news/123456"})
// this.$router.push({name:"start",params:{id:12345}})
this.$router.push({path:"/",query:{keyword:'cc'}})
},
replacePage(){
this.$router.replace({path:"/",query:{keyword:'cc'}})
},
}
}
</script>
<style scoped>
</style>