路由编程式的开发
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router@2.0.0/dist/vue-router.js"></script>
<title>Document</title>
</head>
<body>
<div id="app">
<button v-on:click="showHome">返回首页</button>
<button v-on:click="showOther">显示其他</button>
<router-view></router-view>
</div>
<script>
const Home = {
template: `<div>我是返回首页</div>`
}
const Other = {
template: `<div>我是别的页面</div>`
}
const router = new VueRouter({
routes: [
{ path: "/home", component: Home },
{ path: "/other", component: Other },
]
})
var app = new Vue({
el: "#app",
router: router,
methods: {
showHome: function() {
this.$router.push({ path: '/home' })
},
showOther: function() {
this.$router.push({ path: '/other' })
},
}
})
</script>
</body>
</html>