1. 代码演示
<!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>
</head>
<body>
<div id="app">
{{ msg }}
<!-- 路由占位 -->
<router-view></router-view>
</div>
<script src="vue.js"></script>
<script src="vue-router.js"></script>
<script>
// 登陆组件配置项
var LoginComponent = {
template: `<div>{{ msg }}</div>`,
data() {
return { msg: "我是登陆页面" }
}
};
// 注册组件配置项
var RegisterComponent = {
template: `<div>{{ msg }}</div>`,
data() {
return { msg: "我是注册页面" }
}
};
var vm = new Vue({
el: '#app',
data: {
msg: '路由演示'
},
// 路由配置
router: new VueRouter({
// 路线配置
routes: [
{ path: '/login', component: LoginComponent },
{ path: '/register', component: RegisterComponent }
]
}),
watch: {
$route(to, from) {
console.log(to);
console.log(from);
console.log('路由发生了改变');
}
},
// 组件初始化完毕, 数据也绑定完毕后执行
created() {
console.log(this);
}
});
</script>
</body>
</html>