Skip to content

Fix active route matching #1101

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

Merged
merged 1 commit into from
Jan 18, 2017
Merged
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
2 changes: 1 addition & 1 deletion docs/en/api/router-link.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@

- default: `false`

The default active class matching behavior is **inclusive match**. For example, `<router-link to="/a">` will get this class applied as long as the current path starts with `/a`.
The default active class matching behavior is **inclusive match**. For example, `<router-link to="/a">` will get this class applied as long as the current path starts with `/a/` or is `/a`.

One consequence of this is that `<router-link to="/">` will be active for every route! To force the link into "exact match mode", use the `exact` prop:

Expand Down
7 changes: 5 additions & 2 deletions src/util/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import { stringifyQuery } from './query'

const trailingSlashRE = /\/?$/

export function createRoute (
record: ?RouteRecord,
location: Location,
Expand Down Expand Up @@ -41,7 +43,6 @@ function getFullPath ({ path, query = {}, hash = '' }) {
return (path || '/') + stringifyQuery(query) + hash
}

const trailingSlashRE = /\/$/
export function isSameRoute (a: Route, b: ?Route): boolean {
if (b === START) {
return a === b
Expand Down Expand Up @@ -76,7 +77,9 @@ function isObjectEqual (a = {}, b = {}): boolean {

export function isIncludedRoute (current: Route, target: Route): boolean {
return (
current.path.indexOf(target.path.replace(/\/$/, '')) === 0 &&
current.path.replace(trailingSlashRE, '/').indexOf(
target.path.replace(trailingSlashRE, '/')
) === 0 &&
(!target.hash || current.hash === target.hash) &&
queryIncludes(current.query, target.query)
)
Expand Down
11 changes: 11 additions & 0 deletions test/unit/specs/route.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,16 @@ describe('Route utils', () => {
expect(isIncludedRoute(a, d)).toBe(false)
expect(isIncludedRoute(a, e)).toBe(false)
})

it('trailing slash', () => {
const a = { path: 'user/1' }
const b = { path: 'user/10' }
const c = { path: 'user/10/' }
const d = { path: 'user/1/' }
expect(isIncludedRoute(a, b)).toBe(false)
expect(isIncludedRoute(a, c)).toBe(false)
expect(isIncludedRoute(b, d)).toBe(false)
expect(isIncludedRoute(c, d)).toBe(false)
})
})
})