Skip to content

Commit 965bfcf

Browse files
committed
support event prop on <router-link> (close #785, #786)
1 parent 5950fb3 commit 965bfcf

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

examples/basic/app.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ new Vue({
3434
<li><router-link to="/">/</router-link></li>
3535
<li><router-link to="/foo">/foo</router-link></li>
3636
<li><router-link to="/bar">/bar</router-link></li>
37-
<router-link tag="li" to="/bar">/bar</router-link>
37+
<router-link tag="li" to="/bar" :event="['mousedown', 'touchstart']">
38+
<a>/bar</a>
39+
</router-link>
3840
</ul>
3941
<router-view class="view"></router-view>
4042
</div>

src/components/link.js

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ export default {
2020
exact: Boolean,
2121
append: Boolean,
2222
replace: Boolean,
23-
activeClass: String
23+
activeClass: String,
24+
event: {
25+
type: [String, Array],
26+
default: 'click'
27+
}
2428
},
2529
render (h: Function) {
2630
const router = this.$router
@@ -33,23 +37,8 @@ export default {
3337
? isSameRoute(current, compareTarget)
3438
: isIncludedRoute(current, compareTarget)
3539

36-
const on = {
37-
click: (e) => {
38-
// don't redirect with control keys
39-
/* istanbul ignore if */
40-
if (e.metaKey || e.ctrlKey || e.shiftKey) return
41-
// don't redirect when preventDefault called
42-
/* istanbul ignore if */
43-
if (e.defaultPrevented) return
44-
// don't redirect on right click
45-
/* istanbul ignore if */
46-
if (e.button !== 0) return
47-
// don't redirect if `target="_blank"`
48-
/* istanbul ignore if */
49-
const target = e.target.getAttribute('target')
50-
if (/\b_blank\b/i.test(target)) return
51-
52-
e.preventDefault()
40+
const handler = e => {
41+
if (guardEvent(e)) {
5342
if (this.replace) {
5443
router.replace(normalizedTo)
5544
} else {
@@ -58,6 +47,13 @@ export default {
5847
}
5948
}
6049

50+
const on = { click: guardEvent }
51+
if (Array.isArray(this.event)) {
52+
this.event.forEach(e => { on[e] = handler })
53+
} else {
54+
on[this.event] = handler
55+
}
56+
6157
const data: any = {
6258
class: classes
6359
}
@@ -86,6 +82,25 @@ export default {
8682
}
8783
}
8884

85+
function guardEvent (e) {
86+
// don't redirect with control keys
87+
/* istanbul ignore if */
88+
if (e.metaKey || e.ctrlKey || e.shiftKey) return
89+
// don't redirect when preventDefault called
90+
/* istanbul ignore if */
91+
if (e.defaultPrevented) return
92+
// don't redirect on right click
93+
/* istanbul ignore if */
94+
if (e.button !== 0) return
95+
// don't redirect if `target="_blank"`
96+
/* istanbul ignore if */
97+
const target = e.target.getAttribute('target')
98+
if (/\b_blank\b/i.test(target)) return
99+
100+
e.preventDefault()
101+
return true
102+
}
103+
89104
function findAnchor (children) {
90105
if (children) {
91106
let child

test/e2e/specs/basic.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ module.exports = {
44
.url('http://localhost:8080/basic/')
55
.waitForElementVisible('#app', 1000)
66
.assert.count('li', 4)
7-
.assert.count('li a', 3)
7+
.assert.count('li a', 4)
88
// assert correct href with base
99
.assert.attributeContains('li:nth-child(1) a', 'href', '/basic/')
1010
.assert.attributeContains('li:nth-child(2) a', 'href', '/basic/foo')
1111
.assert.attributeContains('li:nth-child(3) a', 'href', '/basic/bar')
12+
.assert.attributeContains('li:nth-child(4) a', 'href', '/basic/bar')
1213
.assert.containsText('.view', 'home')
1314

1415
.click('li:nth-child(2) a')
@@ -23,7 +24,7 @@ module.exports = {
2324
.assert.urlEquals('http://localhost:8080/basic/')
2425
.assert.containsText('.view', 'home')
2526

26-
.click('li:nth-child(4)')
27+
.click('li:nth-child(4) a')
2728
.assert.urlEquals('http://localhost:8080/basic/bar')
2829
.assert.containsText('.view', 'bar')
2930

0 commit comments

Comments
 (0)