Vue
HTML
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<div id="app">
<p>
<router-link to="/foo" @click.native="handleFooClick">foo</router-link><br>
<router-link to="/bar" @click.native="handleBarClick">bar</router-link><br>
</p>
<router-view></router-view>
</div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
li {
margin: 8px 0;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
del {
color: rgba(0, 0, 0, 0.3);
}
.menu-item {
display: block;
}
Vue
const Foo = { template: '<div>Foo</div>' }
const Bar = { template: '<div>Bar</div>' }
const router = new VueRouter({
routes: [
{ path: '/foo', component: Foo },
{ path: '/bar', component: Bar }
]
})
new Vue({
el: "#app",
router,
methods: {
handleFooClick: function() {
console.log('this.$route:', this.$route.path)
if (this.$route.path === '/foo') {
router.go()
}
},
handleBarClick: function() {
console.log('this.$route:', this.$route.path)
}
}
})