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({
5454
5555 // Baz implements an in-component canDeactivate hook
5656 { 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 ( )
6465 }
6566} )
6667
Original file line number Diff line number Diff line change @@ -11,8 +11,6 @@ declare type RouterOptions = {
1111 base ? : string ;
1212 linkActiveClass ? : string ;
1313 scrollBehavior ? : Function ;
14- beforeEach ?: Function | Array < ?Function > ;
15- afterEach ?: Function | Array < ?Function > ;
1614}
1715
1816declare type RedirectOption = string | { name : string }
Original file line number Diff line number Diff line change @@ -51,7 +51,7 @@ export class History {
5151 // deactivate guards
5252 extractLeaveGuards ( deactivated ) ,
5353 // global before hooks
54- nomralizeGuards ( this . router . options . beforeEach ) ,
54+ this . router . beforeHooks ,
5555 // activate guards
5656 activated . map ( m => m . beforeEnter )
5757 ) . filter ( _ => _ )
@@ -74,7 +74,7 @@ export class History {
7474 updateRoute ( route : Route ) {
7575 this . current = route
7676 this . cb && this . cb ( route )
77- nomralizeGuards ( this . router . options . afterEach ) . forEach ( hook => {
77+ this . router . afterHooks . forEach ( hook => {
7878 hook && hook ( route )
7979 } )
8080 }
@@ -122,16 +122,6 @@ function resolveQueue (
122122 }
123123}
124124
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-
135125function extractLeaveGuards ( matched : Array < RouteRecord > ) : Array < ?Function > {
136126 return Array . prototype . concat . apply ( [ ] , matched . map ( m => {
137127 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 {
1717 history : HashHistory | HTML5History | AbstractHistory ;
1818 match : Matcher ;
1919 fallback : boolean ;
20+ beforeHooks : Array < ?Function > ;
21+ afterHooks : Array < ?Function > ;
2022
2123 constructor ( options : RouterOptions = { } ) {
2224 this . app = null
2325 this . options = options
26+ this . beforeHooks = [ ]
27+ this . afterHooks = [ ]
2428 this . match = createMatcher ( options . routes || [ ] )
2529
2630 let mode = options . mode || 'hash'
@@ -62,6 +66,14 @@ export default class VueRouter {
6266 } )
6367 }
6468
69+ beforeEach ( fn : Function ) {
70+ this . beforeHooks . push ( fn )
71+ }
72+
73+ afterEach ( fn : Function ) {
74+ this . afterHooks . push ( fn )
75+ }
76+
6577 push ( location : RawLocation ) {
6678 this . history . push ( location )
6779 }
You can’t perform that action at this time.
0 commit comments