Skip to content

Commit 825328e

Browse files
committed
feat(link): exact-path prop
Close #2040
1 parent 97d998f commit 825328e

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

examples/active-links/app.js

+4
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ new Vue({
108108
<li><router-link :to="{ name: 'redirect-gallery' }">/redirect-gallery named (redirect to /gallery)</router-link></li>
109109
<li><router-link to="/redirect-image">/redirect-image (redirect to /gallery/image1)</router-link></li>
110110
<li><router-link :to="{ name: 'redirect-image' }" >/redirect-image named (redirect to /gallery/image1)</router-link></li>
111+
112+
<li><router-link to="/users?one" exact-path>/users?one</router-link></li>
113+
<li><router-link to="/users?two" exact-path>/users?two</router-link></li>
114+
<li><router-link to="/users/nested?two" exact-path>/users/nested?two</router-link></li>
111115
</ul>
112116
<router-view class="view"></router-view>
113117
</div>

src/components/link.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export default {
2828
},
2929
custom: Boolean,
3030
exact: Boolean,
31+
exactPath: Boolean,
3132
append: Boolean,
3233
replace: Boolean,
3334
activeClass: String,
@@ -71,8 +72,8 @@ export default {
7172
? createRoute(null, normalizeLocation(route.redirectedFrom), null, router)
7273
: route
7374

74-
classes[exactActiveClass] = isSameRoute(current, compareTarget)
75-
classes[activeClass] = this.exact
75+
classes[exactActiveClass] = isSameRoute(current, compareTarget, this.exactPath)
76+
classes[activeClass] = this.exact || this.exactPath
7677
? classes[exactActiveClass]
7778
: isIncludedRoute(current, compareTarget)
7879

src/util/route.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,26 @@ function getFullPath (
7070
return (path || '/') + stringify(query) + hash
7171
}
7272

73-
export function isSameRoute (a: Route, b: ?Route): boolean {
73+
export function isSameRoute (a: Route, b: ?Route, onlyPath: ?boolean): boolean {
7474
if (b === START) {
7575
return a === b
7676
} else if (!b) {
7777
return false
7878
} else if (a.path && b.path) {
79-
return (
80-
a.path.replace(trailingSlashRE, '') === b.path.replace(trailingSlashRE, '') &&
79+
const isSamePath = a.path.replace(trailingSlashRE, '') === b.path.replace(trailingSlashRE, '')
80+
return onlyPath ? isSamePath : (
81+
isSamePath &&
8182
a.hash === b.hash &&
8283
isObjectEqual(a.query, b.query)
8384
)
8485
} else if (a.name && b.name) {
8586
return (
8687
a.name === b.name &&
87-
a.hash === b.hash &&
88+
(onlyPath || (
89+
a.hash === b.hash &&
8890
isObjectEqual(a.query, b.query) &&
89-
isObjectEqual(a.params, b.params)
91+
isObjectEqual(a.params, b.params))
92+
)
9093
)
9194
} else {
9295
return false

test/e2e/specs/active-links.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = {
1010
browser
1111
.url('http://localhost:8080/active-links/')
1212
.waitForElementVisible('#app', 1000)
13-
.assert.count('li a', 19)
13+
.assert.count('li a', 22)
1414
// assert correct href with base
1515
.assert.attributeContains('li:nth-child(1) a', 'href', '/active-links/')
1616
.assert.attributeContains('li:nth-child(2) a', 'href', '/active-links/')
@@ -57,6 +57,11 @@ module.exports = {
5757
assertActiveLinks(18, [1, 12, 13, 15], null, [15])
5858
assertActiveLinks(19, [1, 12, 13, 15], null, [15])
5959

60+
// exact-path
61+
assertActiveLinks(20, [20, 21], null, [20, 21])
62+
assertActiveLinks(21, [20, 21], null, [20, 21])
63+
assertActiveLinks(22, [22], null, [22])
64+
6065
browser.end()
6166

6267
function assertActiveLinks (n, activeA, activeLI, exactActiveA, exactActiveLI) {

0 commit comments

Comments
 (0)