<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="js/vue.js"></script>
<script src="js/vue-router-3.0.1.js"></script>
</head>
<body>
<div id="app">
<!-- 如果在路由中,使用查詢字符串,則不需要修改 路由匹配規則 -->
<router-link to="/login/12/ls">登錄</router-link>
<router-link to="/register">注冊</router-link>
</div>
<script>
var login = {
// this默認指當前組件實例,在插值表達式中this可以省略
template: '<h1>登錄 --- {{ $route.params.id}} --- {{$route.params.name}}</h1>',
created() { //組件的生命周期鈎子函數
console.log(this.$route.params.id);
}
};
var register = {
template: '<h1>注冊組件</h1>'
}
var router = new VueRouter({
routes: [
{ path: '/login/:id/:name',component: login},
{ path: '/register',component: register},
]
})
//創建Vue實例,得到 ViewModel
var vm = new Vue({
el: '#app',
data: {},
methods: {},
router: router, //將路由規則掛載到vm實例中
});
</script>
</body>
</html>