Skip to content

Commit 65009d0

Browse files
committed
fix v-link active class matching for /
1 parent d0c5b1f commit 65009d0

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/directives/link.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ export default function (Vue) {
6161
path = router._normalizePath(path)
6262
this.destination = path
6363
this.activeRE = path
64-
? new RegExp('^' + path.replace(regexEscapeRE, '\\$&') + '\\b')
64+
? path === '/'
65+
? /^\/$/
66+
: new RegExp('^' + path.replace(regexEscapeRE, '\\$&') + '\\b')
6567
: null
6668
this.updateClasses(this.vm.$route.path)
6769
let isAbsolute = path.charAt(0) === '/'
@@ -85,8 +87,7 @@ export default function (Vue) {
8587
let activeClass = router._linkActiveClass
8688
let exactClass = router._linkActiveExactClass
8789
if (this.activeRE &&
88-
this.activeRE.test(path) &&
89-
path !== '/') {
90+
this.activeRE.test(path)) {
9091
_.addClass(el, activeClass)
9192
} else {
9293
_.removeClass(el, activeClass)

test/unit/specs/core.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,33 +219,43 @@ describe('Core', function () {
219219
template:
220220
'<a id="link-a" v-link="{ path: \'/a\' }">Link A</a>' +
221221
'<a id="link-b" v-link="{ path: \'/b\' }">Link B</a>' +
222+
'<a id="link-c" v-link="{ path: \'/\' }">Link C</a>' +
222223
'<router-view></router-view>'
223224
})
224225
router.start(App, el)
225226
el = router.app.$el
226227
var linkA = el.querySelector('#link-a')
227228
var linkB = el.querySelector('#link-b')
229+
var linkC = el.querySelector('#link-c')
230+
expect(linkA.className).toBe('')
231+
expect(linkB.className).toBe('')
232+
expect(linkC.className).toBe('active active-exact')
228233
router.go('/a')
229234
nextTick(function () {
230235
expect(linkA.className).toBe('active active-exact')
231236
expect(linkB.className).toBe('')
237+
expect(linkC.className).toBe('')
232238
router.go('/a/b/c')
233239
nextTick(function () {
234240
expect(linkA.className).toBe('active')
235241
expect(linkB.className).toBe('')
242+
// expect(linkC.className).toBe('')
236243
router.go('/b')
237244
nextTick(function () {
238245
expect(linkA.className).toBe('')
239246
expect(linkB.className).toBe('active active-exact')
247+
expect(linkC.className).toBe('')
240248
router.go('/b/c/d')
241249
nextTick(function () {
242250
expect(linkA.className).toBe('')
243251
expect(linkB.className).toBe('active')
252+
expect(linkC.className).toBe('')
244253
router.go('/bcd')
245254
nextTick(function () {
246255
// #114 should not match
247256
expect(linkA.className).toBe('')
248257
expect(linkB.className).toBe('')
258+
expect(linkC.className).toBe('')
249259
done()
250260
})
251261
})

0 commit comments

Comments
 (0)