Skip to content

router-link exact path matching (#fix 2040) #2278

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 4 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
10 changes: 10 additions & 0 deletions docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ Since it's just a component, it works with `<transition>` and `<keep-alive>`. Wh

Globally configure `<router-link>` default active class for exact matches. Also see [router-link](#router-link).

### linkExactPathActiveClass

> X.Y.Z+

- type: `string`

- default: `"router-link-exact-path-active"`

Globally configure `<router-link>` default active class for exact path matches. Also see [router-link](router-link.md).

### scrollBehavior

- type: `Function`
Expand Down
2 changes: 2 additions & 0 deletions examples/active-links/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ new Vue({
<router-link tag="li" to="/about">
<a>/about (active class on outer element)</a>
</router-link>

<li><router-link to="/?foo=bar" exact-path>/?foo=bar (exact path match)</router-link></li>
</ul>
<router-view class="view"></router-view>
</div>
Expand Down
13 changes: 13 additions & 0 deletions src/components/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ export default {
default: 'a'
},
exact: Boolean,
exactPath: Boolean,
append: Boolean,
replace: Boolean,
activeClass: String,
exactActiveClass: String,
exactPathActiveClass: String,
event: {
type: eventTypes,
default: 'click'
Expand All @@ -36,19 +38,26 @@ export default {
const classes = {}
const globalActiveClass = router.options.linkActiveClass
const globalExactActiveClass = router.options.linkExactActiveClass
const globalExactPathActiveClass = router.options.linkExactPathActiveClass
// Support global empty active class
const activeClassFallback = globalActiveClass == null
? 'router-link-active'
: globalActiveClass
const exactActiveClassFallback = globalExactActiveClass == null
? 'router-link-exact-active'
: globalExactActiveClass
const exactPathActiveClassFallback = globalExactPathActiveClass == null
? 'router-link-exact-path-active'
: globalExactPathActiveClass
const activeClass = this.activeClass == null
? activeClassFallback
: this.activeClass
const exactActiveClass = this.exactActiveClass == null
? exactActiveClassFallback
: this.exactActiveClass
const exactPathActiveClass = this.exactPathActiveClass == null
? exactPathActiveClassFallback
: this.exactPathActiveClass
const compareTarget = location.path
? createRoute(null, location, null, router)
: route
Expand All @@ -58,6 +67,10 @@ export default {
? classes[exactActiveClass]
: isIncludedRoute(current, compareTarget)

if (this.exactPath) {
classes[exactPathActiveClass] = isSameRoute(current, compareTarget, true)
}

const handler = e => {
if (guardEvent(e)) {
if (this.replace) {
Expand Down
6 changes: 3 additions & 3 deletions src/util/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function getFullPath (
return (path || '/') + stringify(query) + hash
}

export function isSameRoute (a: Route, b: ?Route): boolean {
export function isSameRoute (a: Route, b: ?Route, ignoringQuery: ?boolean): boolean {
if (b === START) {
return a === b
} else if (!b) {
Expand All @@ -79,13 +79,13 @@ export function isSameRoute (a: Route, b: ?Route): boolean {
return (
a.path.replace(trailingSlashRE, '') === b.path.replace(trailingSlashRE, '') &&
a.hash === b.hash &&
isObjectEqual(a.query, b.query)
(ignoringQuery || (!ignoringQuery && isObjectEqual(a.query, b.query)))
)
} else if (a.name && b.name) {
return (
a.name === b.name &&
a.hash === b.hash &&
isObjectEqual(a.query, b.query) &&
(ignoringQuery || (!ignoringQuery && isObjectEqual(a.query, b.query))) &&
isObjectEqual(a.params, b.params)
)
} else {
Expand Down
10 changes: 8 additions & 2 deletions test/e2e/specs/active-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
browser
.url('http://localhost:8080/active-links/')
.waitForElementVisible('#app', 1000)
.assert.count('li a', 11)
.assert.count('li a', 12)
// assert correct href with base
.assert.attributeContains('li:nth-child(1) a', 'href', '/active-links/')
.assert.attributeContains('li:nth-child(2) a', 'href', '/active-links/')
Expand All @@ -17,6 +17,7 @@ module.exports = {
.assert.attributeContains('li:nth-child(9) a', 'href', '/active-links/users/evan?foo=bar&baz=qux')
.assert.attributeContains('li:nth-child(10) a', 'href', '/active-links/about')
.assert.attributeContains('li:nth-child(11) a', 'href', '/active-links/about')
.assert.attributeContains('li:nth-child(12) a', 'href', '/active-links/?foo=bar')
.assert.containsText('.view', 'Home')

assertActiveLinks(1, [1, 2], null, [1, 2])
Expand All @@ -30,10 +31,11 @@ module.exports = {
assertActiveLinks(9, [1, 3, 5, 7, 9], null, [9])
assertActiveLinks(10, [1, 10], [11], [10], [11])
assertActiveLinks(11, [1, 10], [11], [10], [11])
assertActiveLinks(12, [], [], [], [], [12])

browser.end()

function assertActiveLinks (n, activeA, activeLI, exactActiveA, exactActiveLI) {
function assertActiveLinks (n, activeA, activeLI, exactActiveA, exactActiveLI, exactPathActiveA) {
browser.click(`li:nth-child(${n}) a`)
activeA.forEach(i => {
browser.assert.cssClassPresent(`li:nth-child(${i}) a`, 'router-link-active')
Expand All @@ -49,6 +51,10 @@ module.exports = {
browser.assert.cssClassPresent(`li:nth-child(${i})`, 'router-link-exact-active')
.assert.cssClassPresent(`li:nth-child(${i})`, 'router-link-active')
})
exactPathActiveA && exactPathActiveA.forEach(i => {
browser.assert.cssClassPresent(`li:nth-child(${i}) a`, 'router-link-exact-path-active')
.assert.cssClassPresent(`li:nth-child(${i}) a`, 'router-link-active')
})
}
}
}
28 changes: 28 additions & 0 deletions test/unit/specs/route.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@ describe('Route utils', () => {
expect(isSameRoute(a, b)).toBe(true)
expect(isSameRoute(a, c)).toBe(false)
})

it('can ignore query', () => {
const a = {
path: '/abc',
query: {}
}
const b = {
path: '/abc',
query: { foo: 'bar' }
}
const c = {
path: '/abc',
query: { foo: 'baz' }
}
const d = {
path: '/xyz',
query: { foo: 'bar' }
}
expect(isSameRoute(a, b)).toBe(false)
expect(isSameRoute(a, c)).toBe(false)
expect(isSameRoute(a, d)).toBe(false)
expect(isSameRoute(a, b, true)).toBe(true)
expect(isSameRoute(a, c, true)).toBe(true)
expect(isSameRoute(a, d, true)).toBe(false)
expect(isSameRoute(b, c)).toBe(false)
expect(isSameRoute(b, c, true)).toBe(true)
expect(isSameRoute(b, d, true)).toBe(false)
})
})

describe('isIncludedRoute', () => {
Expand Down