87 lines
1.7 KiB
HTML
87 lines
1.7 KiB
HTML
<!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="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js"></script>
|
|
<script src="https://unpkg.com/vue-router@3.0.6/dist/vue-router.js"></script>
|
|
<style>
|
|
.router-link-exact-active {
|
|
color: red;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<div id="app">
|
|
|
|
<ul>
|
|
<li>
|
|
<router-link to="/index?name=bmy">首页</router-link>
|
|
</li>
|
|
<li>
|
|
|
|
<router-link to="/about/2">关于我</router-link>
|
|
</li>
|
|
</ul>
|
|
|
|
<div class="content">
|
|
<router-view></router-view>
|
|
</div>
|
|
|
|
|
|
</div>
|
|
</body>
|
|
<script>
|
|
const Index = {
|
|
data() {
|
|
return {
|
|
title: '我是首页组件'
|
|
}
|
|
},
|
|
template: '<div class="Index">{{ title }} {{ $route.query.name }}</div>'
|
|
}
|
|
|
|
const About = {
|
|
data() {
|
|
return {
|
|
title: '我是关于我组件'
|
|
}
|
|
},
|
|
template: '<div class="About">{{ title }} {{ $route.params.ids }}</div>'
|
|
};
|
|
// query params
|
|
const routes = [
|
|
{
|
|
path: '/index',
|
|
name: 'index',
|
|
component: Index
|
|
},
|
|
{
|
|
path: '/about/:ids',
|
|
name: 'about',
|
|
component: About
|
|
}
|
|
];
|
|
|
|
const router = new VueRouter({
|
|
routes // (缩写) 相当于 routes: routes
|
|
});
|
|
|
|
// 全局守卫 一般在这里面做项目的登录校验
|
|
router.beforeEach((to, from, next) => {
|
|
console.log("你要去的页面:", to)
|
|
console.log("你来的页面:",from)
|
|
next()
|
|
})
|
|
|
|
const app = new Vue({
|
|
el: '#app',
|
|
router
|
|
})
|
|
|
|
|
|
</script>
|
|
</html> |