#导航守卫 路由实例(new VueRouter) beforeEach 路由配置项 beforeEnter 组件 beforeRouteEnter
这是哪个函数里的this指向都是undefined
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import Vue from "vue"
import VueRouter from "vue-router"
Vue.use(VueRouter);
import Books from "../components/Books.vue"
import yf from "../components/yj.vue"
import math from "../components/math.vue"
import edu from "../components/edu.vue"
import internet from "../components/internet.vue"
import ad from "@/components/ad.vue"
let Foods = () => import("../components/Foods.vue")
// 路由懒加载
let router = new VueRouter({
routes : [
{
path : "/books",
name : "books",
meta : {
index : 2
},
beforeEnter (to, from, next) {// 导航守卫第二层
console.log(to,from);
next(false)
},
component : Books
},
{
path : "/foods",
name : "foods",
meta : {
index : 1,
flag : false
},
component : Foods,
children : [
{
path : "edu",
component : edu,
name : "edu"
},
{
path : "math",
component : math,
name : "math"
},
{
path : "internet",
component : internet,
name : "internet"
}
]
},
{
path : "/yf",
name : "yf",
meta : {
index : 3
},
component : yf
},
{
path : "*",
redirect : "/foods"
},
],
linkActiveClass : 'active',
linkExactActiveClass : 'exact'
});
router.beforeEach(function (to,from,next) {// 导航守卫第一层
if(to.path == '/foods/edu'){
if(to.matched[0].meta.flag) {
next();
}else {
next(false);
}
}else if(to.path == '/foods/math'){
if(!to.matched[0].meta.flag) {
next();
}else {
next(false);
}
}else {
next();
}
});
export default router
yf.vue
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
<template>
<div class="wrapper">
<div>这是yj</div>
</div>
</template>
<script>
export default {
name : "yj",
components : {
},
beforeRouteEnter (to, from, next) {// 导航守卫第三层
console.log(to, from);
next();
}
}
</script>
<style>
</style>