Skip to content

Commit e03c866

Browse files
committed
fix v-link active class matching (fix #114)
1 parent 0225e70 commit e03c866

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/directives/link.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { warn } from '../util'
2+
const regexEscapeRE = /[-.*+?^${}()|[\]\/\\]/g
23

34
// install v-link, which provides navigation support for
45
// HTML5 history mode
@@ -46,7 +47,9 @@ export default function (Vue) {
4647
let router = this.vm.$route.router
4748
let activeClass = router._linkActiveClass
4849
let exactClass = activeClass + '-exact'
49-
if (path.indexOf(dest) === 0 && path !== '/') {
50+
if (this.activeRE &&
51+
this.activeRE.test(path) &&
52+
path !== '/') {
5053
_.addClass(el, activeClass)
5154
} else {
5255
_.removeClass(el, activeClass)
@@ -60,6 +63,9 @@ export default function (Vue) {
6063

6164
update: function (path) {
6265
this.destination = path
66+
this.activeRE = path
67+
? new RegExp('^' + path.replace(regexEscapeRE, '\\$&') + '\\b')
68+
: null
6369
this.updateClasses(this.vm.$route.path)
6470
path = path || ''
6571
let router = this.vm.$route.router

test/unit/specs/core.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,13 @@ describe('Core', function () {
240240
nextTick(function () {
241241
expect(linkA.className).toBe('')
242242
expect(linkB.className).toBe('active')
243-
done()
243+
router.go('/bcd')
244+
nextTick(function () {
245+
// #114 should not match
246+
expect(linkA.className).toBe('')
247+
expect(linkB.className).toBe('')
248+
done()
249+
})
244250
})
245251
})
246252
})

0 commit comments

Comments
 (0)