File tree Expand file tree Collapse file tree 4 files changed +22
-21
lines changed
examples/navigation-guards Expand file tree Collapse file tree 4 files changed +22
-21
lines changed Original file line number Diff line number Diff line change @@ -54,13 +54,14 @@ const router = new VueRouter({
54
54
55
55
// Baz implements an in-component canDeactivate hook
56
56
{ path : '/baz' , component : Baz }
57
- ] ,
58
- beforeEach ( route , redirect , next ) {
59
- if ( route . matched . some ( m => m . meta . needGuard ) ) {
60
- guardRoute ( route , redirect , next )
61
- } else {
62
- next ( )
63
- }
57
+ ]
58
+ } )
59
+
60
+ router . beforeEach ( ( route , redirect , next ) => {
61
+ if ( route . matched . some ( m => m . meta . needGuard ) ) {
62
+ guardRoute ( route , redirect , next )
63
+ } else {
64
+ next ( )
64
65
}
65
66
} )
66
67
Original file line number Diff line number Diff line change @@ -11,8 +11,6 @@ declare type RouterOptions = {
11
11
base ? : string ;
12
12
linkActiveClass ? : string ;
13
13
scrollBehavior ? : Function ;
14
- beforeEach ?: Function | Array < ?Function > ;
15
- afterEach ?: Function | Array < ?Function > ;
16
14
}
17
15
18
16
declare type RedirectOption = string | { name : string }
Original file line number Diff line number Diff line change @@ -51,7 +51,7 @@ export class History {
51
51
// deactivate guards
52
52
extractLeaveGuards ( deactivated ) ,
53
53
// global before hooks
54
- nomralizeGuards ( this . router . options . beforeEach ) ,
54
+ this . router . beforeHooks ,
55
55
// activate guards
56
56
activated . map ( m => m . beforeEnter )
57
57
) . filter ( _ => _ )
@@ -74,7 +74,7 @@ export class History {
74
74
updateRoute ( route : Route ) {
75
75
this . current = route
76
76
this . cb && this . cb ( route )
77
- nomralizeGuards ( this . router . options . afterEach ) . forEach ( hook => {
77
+ this . router . afterHooks . forEach ( hook => {
78
78
hook && hook ( route )
79
79
} )
80
80
}
@@ -122,16 +122,6 @@ function resolveQueue (
122
122
}
123
123
}
124
124
125
- function nomralizeGuards ( guards ?: Function | Array < ?Function > ) : Array < ?Function > {
126
- if ( ! guards ) {
127
- return [ ]
128
- }
129
- if ( typeof guards === 'function' ) {
130
- return [ guards ]
131
- }
132
- return guards
133
- }
134
-
135
125
function extractLeaveGuards ( matched : Array < RouteRecord > ) : Array < ?Function > {
136
126
return Array . prototype . concat . apply ( [ ] , matched . map ( m => {
137
127
return Object . keys ( m . components ) . map ( key => {
Original file line number Diff line number Diff line change @@ -17,10 +17,14 @@ export default class VueRouter {
17
17
history : HashHistory | HTML5History | AbstractHistory ;
18
18
match : Matcher ;
19
19
fallback : boolean ;
20
+ beforeHooks : Array < ?Function > ;
21
+ afterHooks : Array < ?Function > ;
20
22
21
23
constructor ( options : RouterOptions = { } ) {
22
24
this . app = null
23
25
this . options = options
26
+ this . beforeHooks = [ ]
27
+ this . afterHooks = [ ]
24
28
this . match = createMatcher ( options . routes || [ ] )
25
29
26
30
let mode = options . mode || 'hash'
@@ -62,6 +66,14 @@ export default class VueRouter {
62
66
} )
63
67
}
64
68
69
+ beforeEach ( fn : Function ) {
70
+ this . beforeHooks . push ( fn )
71
+ }
72
+
73
+ afterEach ( fn : Function ) {
74
+ this . afterHooks . push ( fn )
75
+ }
76
+
65
77
push ( location : RawLocation ) {
66
78
this . history . push ( location )
67
79
}
You can’t perform that action at this time.
0 commit comments