From 8ac478f4f6da314e7bcbdbee8372cc18b1108517 Mon Sep 17 00:00:00 2001 From: zrh122 <46116414+zrh122@users.noreply.github.com> Date: Thu, 9 May 2019 22:24:14 +0800 Subject: [PATCH] fix: make callback of next in beforeRouterEnter more consistent (#2738) Fixes #2761 Closes #2728 --- examples/navigation-guards/app.js | 66 ++++++++++++++++++++++++++--- src/history/base.js | 2 +- test/e2e/specs/navigation-guards.js | 14 +++++- 3 files changed, 73 insertions(+), 9 deletions(-) diff --git a/examples/navigation-guards/app.js b/examples/navigation-guards/app.js index 535d3d5d3..86b2c9496 100644 --- a/examples/navigation-guards/app.js +++ b/examples/navigation-guards/app.js @@ -38,7 +38,10 @@ const Baz = { `, beforeRouteLeave (to, from, next) { - if (this.saved || window.confirm('Not saved, are you sure you want to navigate away?')) { + if ( + this.saved || + window.confirm('Not saved, are you sure you want to navigate away?') + ) { next() } else { next(false) @@ -87,6 +90,43 @@ const Quux = { } } +const NestedParent = { + template: `
Nested Parent
+ /parent/child/1 + /parent/child/2 +
+

+ {{ log }} +

+ +
`, + data: () => ({ logs: [] }), + beforeRouteEnter (to, from, next) { + next(vm => { + vm.logs.push('parent') + }) + } +} + +const GuardMixin = { + beforeRouteEnter (to, from, next) { + next(vm => { + vm.$parent.logs.push('mixin') + }) + } +} + +const NestedChild = { + props: ['n'], + template: `
Child {{ n }}
`, + mixins: [GuardMixin], + beforeRouteEnter (to, from, next) { + next(vm => { + vm.$parent.logs.push('child ' + vm.n) + }) + } +} + const router = new VueRouter({ mode: 'history', base: __dirname, @@ -107,14 +147,25 @@ const router = new VueRouter({ { path: '/qux', component: Qux }, // in-component beforeRouteEnter hook for async components - { path: '/qux-async', component: resolve => { - setTimeout(() => { - resolve(Qux) - }, 0) - } }, + { + path: '/qux-async', + component: resolve => { + setTimeout(() => { + resolve(Qux) + }, 0) + } + }, // in-component beforeRouteUpdate hook - { path: '/quux/:id', component: Quux } + { path: '/quux/:id', component: Quux }, + { + path: '/parent', + component: NestedParent, + children: [ + { path: 'child/1', component: NestedChild, props: { n: 1 }}, + { path: 'child/2', component: NestedChild, props: { n: 2 }} + ] + } ] }) @@ -140,6 +191,7 @@ new Vue({
  • /qux-async
  • /quux/1
  • /quux/2
  • +
  • /parent/child/2
  • diff --git a/src/history/base.js b/src/history/base.js index 5b6f199c6..fbdf25696 100644 --- a/src/history/base.js +++ b/src/history/base.js @@ -297,7 +297,6 @@ function bindEnterGuard ( ): NavigationGuard { return function routeEnterGuard (to, from, next) { return guard(to, from, cb => { - next(cb) if (typeof cb === 'function') { cbs.push(() => { // #750 @@ -308,6 +307,7 @@ function bindEnterGuard ( poll(cb, match.instances, key, isValid) }) } + next(cb) }) } } diff --git a/test/e2e/specs/navigation-guards.js b/test/e2e/specs/navigation-guards.js index 099a2b483..982f6812f 100644 --- a/test/e2e/specs/navigation-guards.js +++ b/test/e2e/specs/navigation-guards.js @@ -3,7 +3,7 @@ module.exports = { browser .url('http://localhost:8080/navigation-guards/') .waitForElementVisible('#app', 1000) - .assert.count('li a', 8) + .assert.count('li a', 9) .assert.containsText('.view', 'home') // alert commands not available in phantom @@ -137,6 +137,18 @@ module.exports = { .click('li:nth-child(7) a') .assert.urlEquals('http://localhost:8080/navigation-guards/quux/1') .assert.containsText('.view', 'id:1 prevId:2') + + // beforeRouteEnter order in children + .click('li:nth-child(9) a') + .assert.urlEquals( + 'http://localhost:8080/navigation-guards/parent/child/2' + ) + .assert.containsText('#bre-order', 'parent mixin child 2') + .click('#nested-parent a') + .assert.urlEquals( + 'http://localhost:8080/navigation-guards/parent/child/1' + ) + .assert.containsText('#bre-order', 'parent mixin child 2 mixin child 1') .end() } }