File tree Expand file tree Collapse file tree 4 files changed +48
-0
lines changed Expand file tree Collapse file tree 4 files changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -66,6 +66,16 @@ export default function (Vue, Router) {
66
66
this . _beforeEachHook = fn
67
67
}
68
68
69
+ /**
70
+ * Set global after hook.
71
+ *
72
+ * @param {Function } fn
73
+ */
74
+
75
+ Router . prototype . afterEach = function ( fn ) {
76
+ this . _afterEachHook = fn
77
+ }
78
+
69
79
/**
70
80
* Navigate to a given path.
71
81
* The path is assumed to be already decoded, and will
Original file line number Diff line number Diff line change @@ -49,6 +49,7 @@ export default class Router {
49
49
this . _previousTransition = null
50
50
this . _notFoundHandler = null
51
51
this . _beforeEachHook = null
52
+ this . _afterEachHook = null
52
53
53
54
// feature detection
54
55
this . _hasPushState =
Original file line number Diff line number Diff line change @@ -194,6 +194,13 @@ export default function (Vue, Router) {
194
194
child . $route = route
195
195
} )
196
196
}
197
+ // call global after hook
198
+ if ( this . _afterEachHook ) {
199
+ this . _afterEachHook . call ( null , {
200
+ to : transition . to ,
201
+ from : transition . from
202
+ } )
203
+ }
197
204
}
198
205
199
206
/**
Original file line number Diff line number Diff line change @@ -446,6 +446,36 @@ describe('Core', function () {
446
446
}
447
447
} )
448
448
449
+ it ( 'global after' , function ( done ) {
450
+ router = new Router ( { abstract : true } )
451
+ var App = Vue . extend ( {
452
+ template : '<div><router-view></router-view></div>'
453
+ } )
454
+ router . map ( {
455
+ '/a' : {
456
+ component : {
457
+ template : '<p>a</p>'
458
+ }
459
+ }
460
+ } )
461
+ var callCount = 0
462
+ router . afterEach ( function ( transition ) {
463
+ if ( callCount === 0 ) {
464
+ // initial match
465
+ expect ( transition . from . path ) . toBeUndefined ( )
466
+ expect ( transition . to . path ) . toBe ( '/' )
467
+ } else {
468
+ // second match
469
+ expect ( transition . from . path ) . toBe ( '/' )
470
+ expect ( transition . to . path ) . toBe ( '/a' )
471
+ done ( )
472
+ }
473
+ callCount ++
474
+ } )
475
+ router . start ( App , el )
476
+ router . go ( '/a' )
477
+ } )
478
+
449
479
it ( 'transitionOnLoad option' , function ( done ) {
450
480
router = new Router ( {
451
481
abstract : true ,
You can’t perform that action at this time.
0 commit comments