1. With Event Modifiers
If you are relying on events, you could try binding with event modifiers and sort of chain them inline. Something like:
<a @click.stop="doThis" @click.right="showContextMenu"></a>
2. Attaching events programmatically
Or, you could create your list of events and their respective implementations to attach with and do the loop with v-on, a workaround from this post:
created() {
const EVENTS = [
{name: 'my-event1', callback: () => console.log('event1')},
{name: 'my-event2', callback: () => console.log('event2')},
{name: 'my-event3', callback: () => console.log('event3')}
]
for (let e of EVENTS) {
this.$on(e.name, e.callback); // Add event listeners
}
}
<button @click="$emit('my-event1')">Raise event1</button>
<button @click="$emit('my-event2')">Raise event2</button>
<button @click="$emit('my-event3')">Raise event3</button>
3. v-on multiple values
Otherwise, just like you can do v-bind on multiple values, you can actually do the same with v-on for events.
<div id="mydDiv" v-on="handlers"></div>
// ...
data() {
const vm = this;
return {
handlers: {
mousedown: vm.divMousedown,
touchstart: vm.divTouchstart
}
}
},
methods: {
divMousedown() {
console.log('event: mousedown');
},
divTouchstart() {
console.log('event: touched');
}
}
If you need to break down the handlers by type of event, try examining the type while the event is being fired, so in your case, since touchstart seems to also trigger mousedown, perhaps:
methods: {
onTouched(evt) {
evt.preventDefault();
if (evt.type === 'mousedown') {
// handle mousedown
}
else if (evt.type === 'touchstart') {
// ...
}
}
}
Note: You might want to call preventDefault() on touchmove rather than touchstart. That way, mouse events can still fire and things like links will continue to work.