@@ -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 _ b l a n k \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 _ b l a n k \b / i. test ( target ) ) return
99+
100+ e . preventDefault ( )
101+ return true
102+ }
103+
89104function findAnchor (children) {
90105 if ( children ) {
91106 let child
0 commit comments