Skip to content

Commit 3c7f007

Browse files
committed
fix in-component enter guard for async components (fix #625)
1 parent 6846da0 commit 3c7f007

File tree

4 files changed

+38
-15
lines changed

4 files changed

+38
-15
lines changed

examples/navigation-guards/app.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,14 @@ const router = new VueRouter({
8181
{ path: '/baz', component: Baz },
8282

8383
// Qux implements an in-component beforeRouteEnter hook
84-
{ path: '/qux', component: Qux }
84+
{ path: '/qux', component: Qux },
85+
86+
// in-component beforeRouteEnter hook for async components
87+
{ path: '/qux-async', component: resolve => {
88+
setTimeout(() => {
89+
resolve(Qux)
90+
}, 0)
91+
} }
8592
]
8693
})
8794

@@ -104,6 +111,7 @@ new Vue({
104111
<li><router-link to="/bar">/bar</router-link></li>
105112
<li><router-link to="/baz">/baz</router-link></li>
106113
<li><router-link to="/qux">/qux</router-link></li>
114+
<li><router-link to="/qux-async">/qux-async</router-link></li>
107115
</ul>
108116
<router-view class="view"></router-view>
109117
</div>

src/history/base.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export class History {
5151
activated
5252
} = resolveQueue(this.current.matched, route.matched)
5353

54-
const postEnterCbs = []
5554
const queue = [].concat(
5655
// in-component leave guards
5756
extractLeaveGuards(deactivated),
@@ -60,27 +59,27 @@ export class History {
6059
// enter guards
6160
activated.map(m => m.beforeEnter),
6261
// async components
63-
resolveAsyncComponents(activated),
64-
// in-component enter guards
65-
extractEnterGuards(activated, postEnterCbs)
66-
).filter(_ => _)
62+
resolveAsyncComponents(activated)
63+
)
6764

6865
this.pending = route
6966
const redirect = location => this.push(location)
67+
const iterator = (hook, next) => hook(route, redirect, next)
7068

71-
runQueue(
72-
queue,
73-
(hook, next) => { hook(route, redirect, next) },
74-
() => {
69+
runQueue(queue, iterator, () => {
70+
const postEnterCbs = []
71+
// wait until async components are resolved before
72+
// extracting in-component enter guards
73+
runQueue(extractEnterGuards(activated, postEnterCbs), iterator, () => {
7574
if (isSameRoute(route, this.pending)) {
7675
this.pending = null
7776
cb(route)
7877
this.router.app.$nextTick(() => {
7978
postEnterCbs.forEach(cb => cb())
8079
})
8180
}
82-
}
83-
)
81+
})
82+
})
8483
}
8584

8685
updateRoute (route: Route) {

src/util/async.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ export function runQueue (queue: Array<any>, fn: Function, cb: Function) {
55
if (index >= queue.length) {
66
cb()
77
} else {
8-
fn(queue[index], () => {
8+
if (queue[index]) {
9+
fn(queue[index], () => {
10+
step(index + 1)
11+
})
12+
} else {
913
step(index + 1)
10-
})
14+
}
1115
}
1216
}
1317
step(0)

test/e2e/specs/navigation-guards.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = {
88
browser
99
.url('http://localhost:8080/navigation-guards/')
1010
.waitForElementVisible('#app', 1000)
11-
.assert.count('li a', 5)
11+
.assert.count('li a', 6)
1212
.assert.containsText('.view', 'home')
1313

1414
.click('li:nth-child(2) a')
@@ -101,12 +101,24 @@ module.exports = {
101101
.assert.urlEquals('http://localhost:8080/navigation-guards/bar')
102102
.assert.containsText('.view', 'bar')
103103

104+
// in-component guard
104105
.click('li:nth-child(5) a')
105106
.assert.urlEquals('http://localhost:8080/navigation-guards/bar')
106107
.assert.containsText('.view', 'bar')
107108
.waitFor(300)
108109
.assert.urlEquals('http://localhost:8080/navigation-guards/qux')
109110
.assert.containsText('.view', 'Qux')
111+
112+
// async component + in-component guard
113+
.click('li:nth-child(1) a')
114+
.assert.urlEquals('http://localhost:8080/navigation-guards/')
115+
.assert.containsText('.view', 'home')
116+
.click('li:nth-child(6) a')
117+
.assert.urlEquals('http://localhost:8080/navigation-guards/')
118+
.assert.containsText('.view', 'home')
119+
.waitFor(300)
120+
.assert.urlEquals('http://localhost:8080/navigation-guards/qux-async')
121+
.assert.containsText('.view', 'Qux')
110122
.end()
111123
}
112124
}

0 commit comments

Comments
 (0)