Skip to content

Commit c50c2fb

Browse files
committed
use new v-link syntax
1 parent 4fdbb70 commit c50c2fb

File tree

6 files changed

+64
-54
lines changed

6 files changed

+64
-54
lines changed

example/advanced/app.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
<div>
1919
<p v-show="authenticating" style="color:red">Authenticating...</p>
2020
<h1>App Header</h1>
21-
<a v-link="/inbox/message/123">inbox</a>
22-
<a v-link="/about">about</a>
23-
<a v-link="/user/1234/profile/what">user</a>
24-
<a v-link="/forbidden">forbidden</a>
21+
<a v-link="{ path: '/inbox/message/123' }">inbox</a>
22+
<a v-link="{ path: '/about' }">about</a>
23+
<a v-link="{ path: '/user/1234/profile/what' }">user</a>
24+
<a v-link="{ path: '/forbidden' }">forbidden</a>
2525
<router-view class="view" v-transition="test" transition-mode="out-in"></router-view>
2626
</div>
2727
</template>

src/directives/link.js

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ export default function (Vue) {
99

1010
Vue.directive('link', {
1111

12-
isLiteral: true,
13-
14-
bind: function () {
12+
bind () {
1513
let vm = this.vm
1614
/* istanbul ignore if */
1715
if (!vm.$route) {
@@ -31,44 +29,21 @@ export default function (Vue) {
3129
}
3230
}
3331
this.el.addEventListener('click', this.handler)
34-
if (!this._isDynamicLiteral) {
35-
this.update(this.expression)
36-
}
3732
// manage active link class
3833
this.unwatch = vm.$watch(
3934
'$route.path',
4035
_.bind(this.updateClasses, this)
4136
)
4237
},
4338

44-
updateClasses: function (path) {
45-
let el = this.el
46-
let dest = this.destination
39+
update (path) {
4740
let router = this.vm.$route.router
48-
let activeClass = router._linkActiveClass
49-
let exactClass = activeClass + '-exact'
50-
if (this.activeRE &&
51-
this.activeRE.test(path) &&
52-
path !== '/') {
53-
_.addClass(el, activeClass)
54-
} else {
55-
_.removeClass(el, activeClass)
56-
}
57-
if (path === dest) {
58-
_.addClass(el, exactClass)
59-
} else {
60-
_.removeClass(el, exactClass)
61-
}
62-
},
63-
64-
update: function (path) {
41+
path = router._normalizePath(path)
6542
this.destination = path
6643
this.activeRE = path
6744
? new RegExp('^' + path.replace(regexEscapeRE, '\\$&') + '\\b')
6845
: null
6946
this.updateClasses(this.vm.$route.path)
70-
path = path || ''
71-
let router = this.vm.$route.router
7247
let isAbsolute = path.charAt(0) === '/'
7348
// do not format non-hash relative paths
7449
let href = router.mode === 'hash' || isAbsolute
@@ -83,7 +58,27 @@ export default function (Vue) {
8358
}
8459
},
8560

86-
unbind: function () {
61+
updateClasses (path) {
62+
let el = this.el
63+
let dest = this.destination
64+
let router = this.vm.$route.router
65+
let activeClass = router._linkActiveClass
66+
let exactClass = activeClass + '-exact'
67+
if (this.activeRE &&
68+
this.activeRE.test(path) &&
69+
path !== '/') {
70+
_.addClass(el, activeClass)
71+
} else {
72+
_.removeClass(el, activeClass)
73+
}
74+
if (path === dest) {
75+
_.addClass(el, exactClass)
76+
} else {
77+
_.removeClass(el, exactClass)
78+
}
79+
},
80+
81+
unbind () {
8782
this.el.removeEventListener('click', this.handler)
8883
this.unwatch && this.unwatch()
8984
}

src/directives/view.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default function (Vue) {
1313

1414
_isRouterView: true,
1515

16-
bind: function () {
16+
bind () {
1717
let route = this.vm.$route
1818
/* istanbul ignore if */
1919
if (!route) {
@@ -74,7 +74,7 @@ export default function (Vue) {
7474
}
7575
},
7676

77-
unbind: function () {
77+
unbind () {
7878
this.router._views.$remove(this)
7979
componentDef.unbind.call(this)
8080
}

src/router/api.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,8 @@ export default function (Vue, Router) {
9090
*/
9191

9292
Router.prototype.go = function (path, replace) {
93-
// handle named routes
94-
if (typeof path === 'object') {
95-
if (path.name) {
96-
var params = path.params || {}
97-
if (path.query) {
98-
params.queryParams = path.query
99-
}
100-
path = this._recognizer.generate(path.name, params)
101-
} else if (path.path) {
102-
path = path.path
103-
}
104-
}
105-
this.history.go(path + '', replace)
93+
path = this._normalizePath(path)
94+
this.history.go(path, replace)
10695
}
10796

10897
/**

src/router/internal.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,32 @@ export default function (Vue, Router) {
238238
}
239239
}
240240

241+
/**
242+
* Normalize named route object / string paths into
243+
* a string.
244+
*
245+
* @param {Object|String|Number} path
246+
* @return {String}
247+
*/
248+
249+
Router.prototype._normalizePath = function (path) {
250+
if (typeof path === 'object') {
251+
if (path.name) {
252+
var params = path.params || {}
253+
if (path.query) {
254+
params.queryParams = path.query
255+
}
256+
return this._recognizer.generate(path.name, params)
257+
} else if (path.path) {
258+
return path.path
259+
} else {
260+
return ''
261+
}
262+
} else {
263+
return path + ''
264+
}
265+
}
266+
241267
/**
242268
* Allow directly passing components to a route
243269
* definition.

test/unit/specs/core.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ describe('Core', function () {
164164
component: {
165165
template:
166166
'<div>' +
167-
'<a id="link-a" v-link="b">Link A</a>' +
167+
'<a id="link-a" v-link="{ path: \'b\' }">Link A</a>' +
168168
'</div>'
169169
}
170170
},
@@ -175,8 +175,8 @@ describe('Core', function () {
175175
},
176176
template:
177177
'<div>' +
178-
'<a id="link-b" v-link="/{{a}}">Link B</a>' +
179-
'<a id="link-c" v-link="{{c}}"></c>' +
178+
'<a id="link-b" v-link="{ path: \'/\' + a }">Link B</a>' +
179+
'<a id="link-c" v-link="{ path: c }"></c>' +
180180
'</div>'
181181
}
182182
}
@@ -216,8 +216,8 @@ describe('Core', function () {
216216
var App = Vue.extend({
217217
replace: false,
218218
template:
219-
'<a id="link-a" v-link="/a">Link A</a>' +
220-
'<a id="link-b" v-link="/b">Link B</a>' +
219+
'<a id="link-a" v-link="{ path: \'/a\' }">Link A</a>' +
220+
'<a id="link-b" v-link="{ path: \'/b\' }">Link B</a>' +
221221
'<router-view></router-view>'
222222
})
223223
router.start(App, el)
@@ -268,7 +268,7 @@ describe('Core', function () {
268268
component: {
269269
template:
270270
'<div>' +
271-
'<a v-link="?id=1234" id="link"></a>' +
271+
'<a v-link="{ path: \'?id=1234\' }" id="link"></a>' +
272272
'{{$route.query.id}}' +
273273
'</div>'
274274
}

0 commit comments

Comments
 (0)