Skip to content

Commit 8ac478f

Browse files
zrh122posva
authored andcommitted
fix: make callback of next in beforeRouterEnter more consistent (#2738)
Fixes #2761 Closes #2728
1 parent 949fe18 commit 8ac478f

File tree

3 files changed

+73
-9
lines changed

3 files changed

+73
-9
lines changed

examples/navigation-guards/app.js

+59-7
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ const Baz = {
3838
</div>
3939
`,
4040
beforeRouteLeave (to, from, next) {
41-
if (this.saved || window.confirm('Not saved, are you sure you want to navigate away?')) {
41+
if (
42+
this.saved ||
43+
window.confirm('Not saved, are you sure you want to navigate away?')
44+
) {
4245
next()
4346
} else {
4447
next(false)
@@ -87,6 +90,43 @@ const Quux = {
8790
}
8891
}
8992

93+
const NestedParent = {
94+
template: `<div id="nested-parent">Nested Parent <hr>
95+
<router-link to="/parent/child/1">/parent/child/1</router-link>
96+
<router-link to="/parent/child/2">/parent/child/2</router-link>
97+
<hr>
98+
<p id="bre-order">
99+
<span v-for="log in logs">{{ log }} </span>
100+
</p>
101+
102+
<router-view/></div>`,
103+
data: () => ({ logs: [] }),
104+
beforeRouteEnter (to, from, next) {
105+
next(vm => {
106+
vm.logs.push('parent')
107+
})
108+
}
109+
}
110+
111+
const GuardMixin = {
112+
beforeRouteEnter (to, from, next) {
113+
next(vm => {
114+
vm.$parent.logs.push('mixin')
115+
})
116+
}
117+
}
118+
119+
const NestedChild = {
120+
props: ['n'],
121+
template: `<div>Child {{ n }}</div>`,
122+
mixins: [GuardMixin],
123+
beforeRouteEnter (to, from, next) {
124+
next(vm => {
125+
vm.$parent.logs.push('child ' + vm.n)
126+
})
127+
}
128+
}
129+
90130
const router = new VueRouter({
91131
mode: 'history',
92132
base: __dirname,
@@ -107,14 +147,25 @@ const router = new VueRouter({
107147
{ path: '/qux', component: Qux },
108148

109149
// in-component beforeRouteEnter hook for async components
110-
{ path: '/qux-async', component: resolve => {
111-
setTimeout(() => {
112-
resolve(Qux)
113-
}, 0)
114-
} },
150+
{
151+
path: '/qux-async',
152+
component: resolve => {
153+
setTimeout(() => {
154+
resolve(Qux)
155+
}, 0)
156+
}
157+
},
115158

116159
// in-component beforeRouteUpdate hook
117-
{ path: '/quux/:id', component: Quux }
160+
{ path: '/quux/:id', component: Quux },
161+
{
162+
path: '/parent',
163+
component: NestedParent,
164+
children: [
165+
{ path: 'child/1', component: NestedChild, props: { n: 1 }},
166+
{ path: 'child/2', component: NestedChild, props: { n: 2 }}
167+
]
168+
}
118169
]
119170
})
120171

@@ -140,6 +191,7 @@ new Vue({
140191
<li><router-link to="/qux-async">/qux-async</router-link></li>
141192
<li><router-link to="/quux/1">/quux/1</router-link></li>
142193
<li><router-link to="/quux/2">/quux/2</router-link></li>
194+
<li><router-link to="/parent/child/2">/parent/child/2</router-link></li>
143195
</ul>
144196
<router-view class="view"></router-view>
145197
</div>

src/history/base.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,6 @@ function bindEnterGuard (
297297
): NavigationGuard {
298298
return function routeEnterGuard (to, from, next) {
299299
return guard(to, from, cb => {
300-
next(cb)
301300
if (typeof cb === 'function') {
302301
cbs.push(() => {
303302
// #750
@@ -308,6 +307,7 @@ function bindEnterGuard (
308307
poll(cb, match.instances, key, isValid)
309308
})
310309
}
310+
next(cb)
311311
})
312312
}
313313
}

test/e2e/specs/navigation-guards.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module.exports = {
33
browser
44
.url('http://localhost:8080/navigation-guards/')
55
.waitForElementVisible('#app', 1000)
6-
.assert.count('li a', 8)
6+
.assert.count('li a', 9)
77
.assert.containsText('.view', 'home')
88

99
// alert commands not available in phantom
@@ -137,6 +137,18 @@ module.exports = {
137137
.click('li:nth-child(7) a')
138138
.assert.urlEquals('http://localhost:8080/navigation-guards/quux/1')
139139
.assert.containsText('.view', 'id:1 prevId:2')
140+
141+
// beforeRouteEnter order in children
142+
.click('li:nth-child(9) a')
143+
.assert.urlEquals(
144+
'http://localhost:8080/navigation-guards/parent/child/2'
145+
)
146+
.assert.containsText('#bre-order', 'parent mixin child 2')
147+
.click('#nested-parent a')
148+
.assert.urlEquals(
149+
'http://localhost:8080/navigation-guards/parent/child/1'
150+
)
151+
.assert.containsText('#bre-order', 'parent mixin child 2 mixin child 1')
140152
.end()
141153
}
142154
}

0 commit comments

Comments
 (0)