Skip to content

feat(history): allow same route navigation #2960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/basic/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import VueRouter from 'vue-router'
Vue.use(VueRouter)

// 2. Define route components
const Home = { template: '<div>home</div>' }
const Home = { template: '<div>home</div>', data: () => { console.log('data reprocessed'); return {} } }
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }
const Unicode = { template: '<div>unicode</div>' }
Expand Down Expand Up @@ -53,7 +53,7 @@ new Vue({
<pre id="counter">{{ n }}</pre>
<pre id="query-t">{{ $route.query.t }}</pre>
<pre id="hash">{{ $route.hash }}</pre>
<router-view class="view"></router-view>
<router-view class="view" :key="$route.id"></router-view>
</div>
`,

Expand Down
17 changes: 6 additions & 11 deletions src/history/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type Router from '../index'
import { inBrowser } from '../util/dom'
import { runQueue } from '../util/async'
import { warn, isError, isExtendedError } from '../util/warn'
import { START, isSameRoute } from '../util/route'
import { START } from '../util/route'
import {
flatten,
flatMapComponents,
Expand Down Expand Up @@ -116,14 +116,6 @@ export class History {
}
onAbort && onAbort(err)
}
if (
isSameRoute(route, current) &&
// in the case the route map has been dynamically appended to
route.matched.length === current.matched.length
) {
this.ensureURL()
return abort(new NavigationDuplicated(route))
}

const { updated, deactivated, activated } = resolveQueue(
this.current.matched,
Expand Down Expand Up @@ -240,15 +232,18 @@ function resolveQueue (
} {
let i
const max = Math.max(current.length, next.length)

// Get the index of the first non-matching route pair
for (i = 0; i < max; i++) {
if (current[i] !== next[i]) {
break
}
}

return {
updated: next.slice(0, i),
activated: next.slice(i),
deactivated: current.slice(i)
activated: next,
deactivated: current
}
}

Expand Down
29 changes: 11 additions & 18 deletions src/history/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,21 @@ export class HashHistory extends History {

push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
const { current: fromRoute } = this
this.transitionTo(
location,
route => {
pushHash(route.fullPath)
handleScroll(this.router, route, fromRoute, false)
onComplete && onComplete(route)
},
onAbort
)
this.transitionTo(location, route => {
const fn = fromRoute.fullPath === route.fullPath ? replaceHash : pushHash
fn(route.fullPath)
handleScroll(this.router, route, fromRoute, false)
onComplete && onComplete(route)
}, onAbort)
}

replace (location: RawLocation, onComplete?: Function, onAbort?: Function) {
const { current: fromRoute } = this
this.transitionTo(
location,
route => {
replaceHash(route.fullPath)
handleScroll(this.router, route, fromRoute, false)
onComplete && onComplete(route)
},
onAbort
)
this.transitionTo(location, route => {
replaceHash(route.fullPath)
handleScroll(this.router, route, fromRoute, false)
onComplete && onComplete(route)
}, onAbort)
}

go (n: number) {
Expand Down
3 changes: 2 additions & 1 deletion src/history/html5.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export class HTML5History extends History {
push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
const { current: fromRoute } = this
this.transitionTo(location, route => {
pushState(cleanPath(this.base + route.fullPath))
const fn = fromRoute.fullPath === route.fullPath ? replaceState : pushState
fn(cleanPath(this.base + route.fullPath))
handleScroll(this.router, route, fromRoute, false)
onComplete && onComplete(route)
}, onAbort)
Expand Down
1 change: 1 addition & 0 deletions src/util/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function createRoute (
} catch (e) {}

const route: Route = {
id: Date.now(),
name: location.name || (record && record.name),
meta: (record && record.meta) || {},
path: location.path || '/',
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/specs/navigation-guards.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ module.exports = {
.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')
.assert.containsText('#bre-order', 'parent mixin child 2 parent mixin child 1')
.end()
}
}