From 4bb9331d5fb4f37ce339b18a2a75f923b03b9b5c Mon Sep 17 00:00:00 2001 From: wanyaxing <340014824@qq.com> Date: Sun, 26 Nov 2017 20:50:52 +0800 Subject: [PATCH 1/5] add the replace attribute in location for hooking in beforeEach https://github.com/vuejs/vue-router/issues/1090 --- src/history/hash.js | 6 ++++++ src/history/html5.js | 6 ++++++ src/util/route.js | 1 + 3 files changed, 13 insertions(+) diff --git a/src/history/hash.js b/src/history/hash.js index f4a2f3828..523f06d7c 100644 --- a/src/history/hash.js +++ b/src/history/hash.js @@ -55,6 +55,12 @@ export class HashHistory extends History { replace (location: RawLocation, onComplete?: Function, onAbort?: Function) { const { current: fromRoute } = this + if (typeof location === 'string'){ + location = {path:location} + } + if (typeof location === 'object' && !location.replace){ + location.replace = 1 + } this.transitionTo(location, route => { replaceHash(route.fullPath) handleScroll(this.router, route, fromRoute, false) diff --git a/src/history/html5.js b/src/history/html5.js index 95c47344c..ca3595764 100644 --- a/src/history/html5.js +++ b/src/history/html5.js @@ -52,6 +52,12 @@ export class HTML5History extends History { replace (location: RawLocation, onComplete?: Function, onAbort?: Function) { const { current: fromRoute } = this + if (typeof location === 'string'){ + location = {path:location} + } + if (typeof location === 'object' && !location.replace){ + location.replace = 1 + } this.transitionTo(location, route => { replaceState(cleanPath(this.base + route.fullPath)) handleScroll(this.router, route, fromRoute, false) diff --git a/src/util/route.js b/src/util/route.js index 54a91a738..87cba2e42 100644 --- a/src/util/route.js +++ b/src/util/route.js @@ -23,6 +23,7 @@ export function createRoute ( meta: (record && record.meta) || {}, path: location.path || '/', hash: location.hash || '', + replace: location.replace || 0, query, params: location.params || {}, fullPath: getFullPath(location, stringifyQuery), From 474baa1a2463e2c26616f19e2b45f06bcb6869ac Mon Sep 17 00:00:00 2001 From: wanyaxing <340014824@qq.com> Date: Sun, 26 Nov 2017 20:57:59 +0800 Subject: [PATCH 2/5] CircleCI failed fix --- src/history/hash.js | 6 +++--- src/history/html5.js | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/history/hash.js b/src/history/hash.js index 523f06d7c..dcac48c6d 100644 --- a/src/history/hash.js +++ b/src/history/hash.js @@ -55,10 +55,10 @@ export class HashHistory extends History { replace (location: RawLocation, onComplete?: Function, onAbort?: Function) { const { current: fromRoute } = this - if (typeof location === 'string'){ - location = {path:location} + if (typeof location === 'string') { + location = { path: location } } - if (typeof location === 'object' && !location.replace){ + if (typeof location === 'object' && !location.replace) { location.replace = 1 } this.transitionTo(location, route => { diff --git a/src/history/html5.js b/src/history/html5.js index ca3595764..6f0f97882 100644 --- a/src/history/html5.js +++ b/src/history/html5.js @@ -52,10 +52,10 @@ export class HTML5History extends History { replace (location: RawLocation, onComplete?: Function, onAbort?: Function) { const { current: fromRoute } = this - if (typeof location === 'string'){ - location = {path:location} + if (typeof location === 'string') { + location = { path: location } } - if (typeof location === 'object' && !location.replace){ + if (typeof location === 'object' && !location.replace) { location.replace = 1 } this.transitionTo(location, route => { From 538ce600a2cfdce868bb5615f6f079cf71356c76 Mon Sep 17 00:00:00 2001 From: wanyaxing <340014824@qq.com> Date: Mon, 27 Nov 2017 10:13:46 +0800 Subject: [PATCH 3/5] CircleCI failed fix2 --- src/history/hash.js | 2 +- src/history/html5.js | 2 +- src/util/route.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/history/hash.js b/src/history/hash.js index dcac48c6d..5a02c7958 100644 --- a/src/history/hash.js +++ b/src/history/hash.js @@ -59,7 +59,7 @@ export class HashHistory extends History { location = { path: location } } if (typeof location === 'object' && !location.replace) { - location.replace = 1 + (location: Object).replace = true } this.transitionTo(location, route => { replaceHash(route.fullPath) diff --git a/src/history/html5.js b/src/history/html5.js index 6f0f97882..2dfc30297 100644 --- a/src/history/html5.js +++ b/src/history/html5.js @@ -56,7 +56,7 @@ export class HTML5History extends History { location = { path: location } } if (typeof location === 'object' && !location.replace) { - location.replace = 1 + (location: Object).replace = true } this.transitionTo(location, route => { replaceState(cleanPath(this.base + route.fullPath)) diff --git a/src/util/route.js b/src/util/route.js index 87cba2e42..13ce80f9e 100644 --- a/src/util/route.js +++ b/src/util/route.js @@ -23,7 +23,7 @@ export function createRoute ( meta: (record && record.meta) || {}, path: location.path || '/', hash: location.hash || '', - replace: location.replace || 0, + replace: location.replace || false, query, params: location.params || {}, fullPath: getFullPath(location, stringifyQuery), From e533b54c536bbb725c380ba549571bff294364d2 Mon Sep 17 00:00:00 2001 From: wanyaxing <340014824@qq.com> Date: Mon, 18 Dec 2017 20:13:23 +0800 Subject: [PATCH 4/5] replace support in normalizeLocation --- src/util/location.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/util/location.js b/src/util/location.js index 5e482aebe..316df9340 100644 --- a/src/util/location.js +++ b/src/util/location.js @@ -52,10 +52,13 @@ export function normalizeLocation ( hash = `#${hash}` } + const replace = next.replace || false + return { _normalized: true, path, query, + replace, hash } } From d3a51dc12a558ebb86da578c956e91d0943f2f4a Mon Sep 17 00:00:00 2001 From: wanyaxing <340014824@qq.com> Date: Mon, 18 Dec 2017 20:13:29 +0800 Subject: [PATCH 5/5] add the example of replace in navigation guard --- examples/index.html | 1 + examples/push-or-replace/app.js | 73 +++++++++++++++++++++++++++++ examples/push-or-replace/index.html | 6 +++ 3 files changed, 80 insertions(+) create mode 100644 examples/push-or-replace/app.js create mode 100644 examples/push-or-replace/index.html diff --git a/examples/index.html b/examples/index.html index f2bdf7225..d0788851e 100644 --- a/examples/index.html +++ b/examples/index.html @@ -25,6 +25,7 @@

Vue Router Examples

  • Auth Flow
  • Discrete Components
  • Nested Routers
  • +
  • Push Or Replace
  • diff --git a/examples/push-or-replace/app.js b/examples/push-or-replace/app.js new file mode 100644 index 000000000..30439a261 --- /dev/null +++ b/examples/push-or-replace/app.js @@ -0,0 +1,73 @@ +import Vue from 'vue' +import VueRouter from 'vue-router' + +Vue.use(VueRouter) + +const Home = { template: '
    home {{$route.query.time}}
    ' } +const Page = { template: '
    page {{$route.query.time}}
    ' } +const Detail = { template: '
    detail {{$route.query.time}}
    ' } + +const router = new VueRouter({ + mode: 'history', + base: __dirname, + routes: [ + { path: '/', component: Home }, + + { path: '/page', component: Page }, + + { path: '/detail', component: Detail } + + ] +}) + +// User can check the replace type in navigation guard, and do anything they want. +router.beforeEach((to, from, next) => { + if (to.replace) { + to.query.replace = true + } else { + to.query.replace = false + } + + if (to && to.query && !to.query.time) { + to.query.time = new Date().getTime() + next(to) + } else { + next() + } +}) + +new Vue({ + router, + template: ` +
    +

    Push Or Replace

    +

    User can check the replace type in navigation guard, and do anything they want.

    +
    +router.beforeEach((to, from, next) => {
    +  if (to.replace) {
    +    to.query.replace = true
    +  }
    +  else {
    +    to.query.replace = false
    +  }
    +
    +  if (to && to.query && !to.query.time) {
    +    to.query.time = new Date().getTime()
    +    next(to)
    +  } else {
    +    next()
    +  }
    +})
    +      
    + + +
    + ` +}).$mount('#app') diff --git a/examples/push-or-replace/index.html b/examples/push-or-replace/index.html new file mode 100644 index 000000000..ffb26c918 --- /dev/null +++ b/examples/push-or-replace/index.html @@ -0,0 +1,6 @@ + + +← Examples index +
    + +