vuerouter-4.编程式导航

1.router.push

想要导航到不同的 URL,则使用 router.push 方法。这个方法会向 history 栈添加一个新的记录,所以,当用户点击浏览器后退按钮时,则回到之前的 URL。

2.router.replace

跟 router.push 很像,唯一的不同就是,它不会向 history 添加新记录,而是跟它的方法名一样 —— 替换掉当前的 history 记录。

3.router.go()

这个方法的参数是一个整数,意思是在 history 记录中向前或者后退多少步,类似 window.history.go(n)

实例---------------------------------------------------------------------------

<template>
<div id="app">
<img src="./assets/logo.png">
<ul>
<router-link to="/h" tag="li">HelloWorld</router-link>
<router-link to="/learn" tag="li">learn</router-link>
</ul>
<button class="btn" @click="gotoHello">去HelloWorld</button>
<router-view />
</div>
</template>

<script>
export default {
name: 'App',
components: {

},
methods: {
gotoHello() {
//this.$router.push("/h")
//this.$router.replace("/h")
this.$router.go(-1)
}
}
}
</script>

<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}

ul li {
text-align: center;
display: inline-block;
text-decoration: underline;
cursor: pointer;
color: blue;
}

ul li+li {
margin-left: 20px;
}

.btn {
border: 1px solid #fff;
padding: 5px;
color: #fff;
background-color: #0000FF;
cursor: pointer;
}
</style>

猜你喜欢

转载自www.cnblogs.com/xiao-peng-ji/p/11336310.html