226 lines
6.0 KiB
HTML
226 lines
6.0 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en">
|
||
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>Document</title>
|
||
<style>
|
||
.bounce-enter-active {
|
||
animation: bounce-in .5s;
|
||
}
|
||
|
||
.bounce-leave-active {
|
||
animation: bounce-in .5s reverse;
|
||
}
|
||
|
||
@keyframes bounce-in {
|
||
0% {
|
||
transform: scale(0);
|
||
}
|
||
|
||
50% {
|
||
transform: scale(1.5);
|
||
}
|
||
|
||
100% {
|
||
transform: scale(1);
|
||
}
|
||
}
|
||
</style>
|
||
</head>
|
||
|
||
<body>
|
||
<div id="app">
|
||
<h1>Hello App!</h1>
|
||
<p>
|
||
<!-- 使用 router-link 组件来导航. -->
|
||
<!-- 通过传入 `to` 属性指定链接. -->
|
||
<!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
|
||
<router-link to="/index">Go to index</router-link>
|
||
<router-link to="/bar">Go to Bar</router-link>
|
||
<router-link to="/me">我的</router-link>
|
||
</p>
|
||
<!-- 路由出口 -->
|
||
<!-- 路由匹配到的组件将渲染在这里 -->
|
||
<transition name="bounce">
|
||
<router-view></router-view>
|
||
</transition>
|
||
|
||
</div>
|
||
</body>
|
||
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
|
||
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
|
||
<script>
|
||
|
||
// 0. 如果使用模块化机制编程,导入Vue和VueRouter,要调用 Vue.use(VueRouter)
|
||
|
||
// 1. 定义 (路由) 组件。
|
||
// 可以从其他文件 import 进来
|
||
|
||
// 参数传递,有两种方式
|
||
// query 传参 支持向 path 和 name
|
||
// http://a.com ?id=1&page=2
|
||
// :to="{ path: '/info', query: { id: v.id, name: v.name } }"
|
||
// 获取 query 传参
|
||
// this.$route.query.参数名
|
||
|
||
// params 传参 只支持向 name 进行跳转
|
||
// http://a.com/1/2
|
||
// :to="{ name: 'info', params: { id: v.id, name: v.name } }"
|
||
// path: '/info/:id/:name',
|
||
// 获取 params 传参
|
||
// this.$route.params.参数名
|
||
const Index = {
|
||
template: `
|
||
<div class="index">
|
||
<ul>
|
||
<li v-for="v,i in list" :key="i">
|
||
<router-link :to="{ name: 'info', params: { id: v.id, name: v.name } }">{{ v.name }}</router-link>
|
||
</li>
|
||
</ul>
|
||
<div @click="goto">点我跳转</div>
|
||
</div>
|
||
`,
|
||
data() {
|
||
return {
|
||
list: [
|
||
{ name: '测试新闻1', id: 1 },
|
||
{ name: '测试新闻2', id: 2 },
|
||
{ name: '测试新闻3', id: 3 },
|
||
{ name: '测试新闻4', id: 4 },
|
||
]
|
||
}
|
||
},
|
||
methods: {
|
||
goto() {
|
||
this.$router.push({
|
||
name: 'bar',
|
||
query: {
|
||
id: 1
|
||
}
|
||
})
|
||
}
|
||
}
|
||
}
|
||
const Bar = { template: '<div>bar</div>' }
|
||
const Info = {
|
||
template: `
|
||
<div class="info">
|
||
<h2>{{msg}}</h2>
|
||
</div>
|
||
`,
|
||
created() {
|
||
//console.log(this.$route.query.id);
|
||
//console.log(this.$route.query.name);
|
||
console.log(this.$route.params);
|
||
},
|
||
data() {
|
||
return {
|
||
msg: '详情页'
|
||
}
|
||
}
|
||
}
|
||
|
||
const Login = { template: '<div>Login</div>' }
|
||
const Me = { template: '<div>Me</div>' }
|
||
|
||
// 2. 定义路由
|
||
// 每个路由应该映射一个组件。 其中"component" 可以是
|
||
// 通过 Vue.extend() 创建的组件构造器,
|
||
// 或者,只是一个组件配置对象。
|
||
// 我们晚点再讨论嵌套路由。
|
||
const routes = [
|
||
{
|
||
path: '/index',
|
||
component: Index,
|
||
name: 'index',
|
||
meta: {
|
||
isLogin: false,
|
||
title: '首页'
|
||
}
|
||
},
|
||
{
|
||
path: '/bar',
|
||
component: Bar,
|
||
name: 'bar',
|
||
redirect: '/index',
|
||
meta: {
|
||
isLogin: false,
|
||
title: 'Bar页面'
|
||
}
|
||
},
|
||
{
|
||
path: '/info/:id/:name',
|
||
component: Info,
|
||
name: 'info',
|
||
meta: {
|
||
isLogin: false,
|
||
title: '详情'
|
||
}
|
||
},
|
||
{
|
||
path: '/login',
|
||
component: Login,
|
||
name: 'login',
|
||
meta: {
|
||
isLogin: false,
|
||
title: '登录'
|
||
}
|
||
},
|
||
{
|
||
path: '/me',
|
||
component: Me,
|
||
name: 'me',
|
||
meta: {
|
||
isLogin: true,
|
||
title: '我的'
|
||
}
|
||
}
|
||
]
|
||
|
||
// 3. 创建 router 实例,然后传 `routes` 配置
|
||
// 你还可以传别的配置参数, 不过先这么简单着吧。
|
||
const router = new VueRouter({
|
||
routes // (缩写) 相当于 routes: routes
|
||
});
|
||
|
||
router.beforeEach((to, from, next) => {
|
||
document.title = to.meta.title
|
||
// true 需要登录
|
||
if (to.meta.isLogin) {
|
||
// 判断是否登录,登录过了,正常查看页面
|
||
// 如果没登录,去登录
|
||
if (localStorage.getItem("token") != null) {
|
||
next()
|
||
} else {
|
||
next({
|
||
path: '/login',
|
||
query: {
|
||
from: from.fullPath
|
||
}
|
||
})
|
||
}
|
||
// false 不需要登录,正常查看页面
|
||
} else {
|
||
next();
|
||
}
|
||
})
|
||
|
||
// 4. 创建和挂载根实例。
|
||
// 记得要通过 router 配置参数注入路由,
|
||
// 从而让整个应用都有路由功能
|
||
const app = new Vue({
|
||
el: '#app',
|
||
router,
|
||
|
||
});
|
||
|
||
|
||
// 导航守卫
|
||
// 全局
|
||
// 路由独享的守卫
|
||
// 组件内的守卫
|
||
</script>
|
||
|
||
</html> |