来人啊,上代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
<div id="app">
<p>
<button @click="gohome">/user/foo</button>
<button @click="gokkk">/user/foo/kkk</button>
<button @click="goqqq">/user/foo/qqq</button>
</p>
<router-view></router-view>
</div>
<script type="text/javascript">
const Userhome = {
template:`
<div class="user">
<h2>Userhome</h2>
</div>
`
}
const Userkkk = {
template:`
<div class="user">
<h2>Userkkk</h2>
</div>
`
}
const Userqqq = {
template:`
<div class="user">
<h2>Userqqq</h2>
</div>
`
}
const router = new VueRouter({
routes:[
{
path:'/user/foo',
component:Userhome
},
{
path:'/user/kkk',
component:Userkkk
},
{
path:'/user/qqq',
component:Userqqq
}
]
})
//默认加载第一个路径
router.push("/user/foo");
/*const app = new Vue({
router//ES6解构函数的写法相当于 router:router
}).$mount('#app')*/
const app = new Vue({
el:'#app',
methods:{
gohome(){
//页面跳转,push是增加到历史管理
this.$router.history.push({path:'foo'})
},
gokkk(){
//
this.$router.history.push({path:'kkk'});
},
goqqq(){
this.$router.history.push({path:'qqq'});
}
},
router:router
})
</script>
</body>
</html>