Skip to content

Commit 4da7021

Browse files
committed
fix(abstract): call afterHooks with go
Fix #3250
1 parent 036fced commit 4da7021

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/history/abstract.js

+4
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ export class AbstractHistory extends History {
4646
this.confirmTransition(
4747
route,
4848
() => {
49+
const prev = this.current
4950
this.index = targetIndex
5051
this.updateRoute(route)
52+
this.router.afterHooks.forEach(hook => {
53+
hook && hook(route, prev)
54+
})
5155
},
5256
err => {
5357
if (isNavigationFailure(err, NavigationFailureType.duplicated)) {
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import Vue from 'vue'
2+
import VueRouter from '../../../src/index'
3+
4+
Vue.use(VueRouter)
5+
6+
const delay = t => new Promise(resolve => setTimeout(resolve, t))
7+
8+
describe('abstract history', () => {
9+
it('run afterEach after initial navigation', done => {
10+
const router = new VueRouter({ mode: 'abstract' })
11+
const afterEach = jasmine.createSpy('afterEach')
12+
const onReady = jasmine.createSpy('ready')
13+
const onError = jasmine.createSpy('error')
14+
router.afterEach(afterEach)
15+
router.onReady(onReady, onError)
16+
17+
router.push('/').finally(() => {
18+
expect(onReady).toHaveBeenCalled()
19+
expect(onError).not.toHaveBeenCalled()
20+
expect(afterEach).toHaveBeenCalled()
21+
done()
22+
})
23+
})
24+
25+
it('run afterEach after router.go', done => {
26+
const router = new VueRouter({ mode: 'abstract' })
27+
const afterEach = jasmine.createSpy('afterEach')
28+
29+
router
30+
.push('/')
31+
.then(() => router.push('/foo'))
32+
.then(() => {
33+
router.afterEach(afterEach)
34+
router.go(-1)
35+
return delay(30)
36+
})
37+
.finally(() => {
38+
expect(afterEach).toHaveBeenCalled()
39+
done()
40+
})
41+
})
42+
})

0 commit comments

Comments
 (0)