From 736c2ce0bd6ad651b6d22d04cadb6dc01de6a527 Mon Sep 17 00:00:00 2001 From: zrh122 <1229550935@qq.com> Date: Wed, 24 Apr 2019 16:45:21 +0800 Subject: [PATCH 1/3] chore: make callback of next in beforeRouterEnter more consistent --- src/history/base.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) }) } } From dd4a787930a6bb01cdbb29105e860cf9f90ae77d Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 9 May 2019 16:08:58 +0200 Subject: [PATCH 2/3] test: add nested beforeRouteEnter --- examples/navigation-guards/app.js | 57 +++++++++++++++++++++++++---- test/e2e/specs/navigation-guards.js | 14 ++++++- 2 files changed, 63 insertions(+), 8 deletions(-) diff --git a/examples/navigation-guards/app.js b/examples/navigation-guards/app.js index fb83a2f21..647c3214c 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,34 @@ 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 NestedChild = { + props: ['n'], + template: `
Child {{ n }}
`, + beforeRouteEnter (to, from, next) { + next(vm => { + vm.$parent.logs.push('child ' + vm.n) + }) + } +} + const router = new VueRouter({ mode: 'history', base: __dirname, @@ -107,14 +138,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 +182,7 @@ new Vue({
  • /qux-async
  • /quux/1
  • /quux/2
  • +
  • /parent/child/2
  • diff --git a/test/e2e/specs/navigation-guards.js b/test/e2e/specs/navigation-guards.js index 4b75c5aeb..79fd7ed84 100644 --- a/test/e2e/specs/navigation-guards.js +++ b/test/e2e/specs/navigation-guards.js @@ -8,7 +8,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') .click('li:nth-child(2) a') @@ -130,6 +130,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 child 2') + .click('#nested-parent a') + .assert.urlEquals( + 'http://localhost:8080/navigation-guards/parent/child/1' + ) + .assert.containsText('#bre-order', 'parent child 2 child 1') .end() } } From b589bf27e518c272ca15f6c4dad35d8d4d6155ac Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 9 May 2019 16:20:25 +0200 Subject: [PATCH 3/3] test: add test case for mixin beforeRouteEnter --- examples/navigation-guards/app.js | 9 +++++++++ test/e2e/specs/navigation-guards.js | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/examples/navigation-guards/app.js b/examples/navigation-guards/app.js index 519c2be67..86b2c9496 100644 --- a/examples/navigation-guards/app.js +++ b/examples/navigation-guards/app.js @@ -108,9 +108,18 @@ const NestedParent = { } } +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) diff --git a/test/e2e/specs/navigation-guards.js b/test/e2e/specs/navigation-guards.js index 73229e1c5..982f6812f 100644 --- a/test/e2e/specs/navigation-guards.js +++ b/test/e2e/specs/navigation-guards.js @@ -143,12 +143,12 @@ module.exports = { .assert.urlEquals( 'http://localhost:8080/navigation-guards/parent/child/2' ) - .assert.containsText('#bre-order', '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 child 2 child 1') + .assert.containsText('#bre-order', 'parent mixin child 2 mixin child 1') .end() } }