forked from zhouyupeng/vue-cli3-H5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
71 lines (69 loc) · 1.79 KB
/
router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'
import store from 'store/index'
Vue.use(Router)
const router = new Router({
routes: [
{
path: '/',
name: 'home',
component: Home,
meta: {
auth: false, // 是否需要登录
keepAlive: true // 是否缓存组件
}
},
{
path: '/about',
name: 'about',
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () =>
import(/* webpackChunkName: "about" */ './views/About.vue'),
meta: {
auth: true,
keepAlive: true
}
},
{
path: '/login',
name: 'login',
component: () =>
import(/* webpackChunkName: "login" */ './views/login.vue'),
meta: {
auth: false,
keepAlive: true
}
},
{
path: '*', // 未匹配到路由时重定向
redirect: '/',
meta: {
// auth: true,
// keepAlive: true
}
}
]
})
// 全局路由钩子函数 对全局有效
router.beforeEach((to, from, next) => {
let auth = to.meta.auth
let token = store.getters['login/token'];
if (auth) { // 需要登录
if (token) {
next()
} else {
next({
name: 'login',
query: {
redirect: to.path
}
})
}
} else {
next()
}
})
export default router