Skip to content

Commit 4d2195a

Browse files
committed
router-link active class
1 parent 5ab354d commit 4d2195a

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/components/link.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { cleanPath, getFullPath } from '../util/path'
2-
import { normalizeLocation } from '../util/location'
2+
import { normalizeLocation, isSameLocation } from '../util/location'
33

44
export default {
55
name: 'router-link',
@@ -13,20 +13,26 @@ export default {
1313
default: 'a'
1414
},
1515
append: Boolean,
16-
replace: Boolean
16+
replace: Boolean,
17+
activeClass: {
18+
type: String,
19+
default: 'router-link-active'
20+
}
1721
},
1822
render (h) {
1923
const router = this.$router
2024
const current = router.history.current
2125
const to = normalizeLocation(this.to, current, this.append)
22-
const fullPath = to.name
23-
? router.match(to).fullPath
24-
: getFullPath(to)
26+
const resolved = router.match(to)
27+
const fullPath = resolved.fullPath
2528
const base = router.history.base
2629
const href = base ? cleanPath(base + fullPath) : fullPath
30+
const classes = {}
31+
classes[this.activeClass] = isSameLocation(resolved, this.$route)
2732

2833
return h(this.tag, {
2934
attrs: { href },
35+
class: classes,
3036
on: {
3137
click: (e) => {
3238
e.preventDefault()

0 commit comments

Comments
 (0)