Skip to content

Commit

Permalink
add active link class
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jul 17, 2015
1 parent 2dacb0b commit 0e11456
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions example/index.html
Expand Up @@ -11,6 +11,9 @@
opacity: 0;
transform: translate3d(10px, 0, 0);
}
.v-link-active {
color: red;
}
[v-cloak] {
display: none;
}
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Expand Up @@ -45,6 +45,7 @@ function Router (options) {
this._hashbang = options.hashbang !== false
this._history = !!(this._hasPushState && options.history)
this._saveScrollPosition = !!options.saveScrollPosition
this._linkActiveClass = options.linkActiveClass || 'v-link-active'

// resolve root path
var root = options && options.root
Expand Down
10 changes: 10 additions & 0 deletions src/link.js
Expand Up @@ -31,10 +31,20 @@ module.exports = function (Vue) {
if (!this._isDynamicLiteral) {
this.update(this.expression)
}
// manage active link class
var activeClass = vm.route._router._linkActiveClass
this.unwatch = vm.$watch('route.path', function (path) {
if (path === self.destination) {
_.addClass(self.el, activeClass)
} else {
_.removeClass(self.el, activeClass)
}
})
},

unbind: function () {
this.el.removeEventListener('click', this.handler)
this.unwatch && this.unwatch()
},

update: function (path) {
Expand Down

0 comments on commit 0e11456

Please sign in to comment.