问题
I have a number of divs formatted to be buttons (20ish in total).
I'd like to have a single click listener (possibly on the parent div) that would listen for any/all clicks, and capture that specific divs innerHTML.
Other than vanilla JS or jQuery–in which I could listen to an id or class–is there a way with Vue.js?
Thanks!
回答1:
Yes you can accomplish this easily with an event bus:
var bus = new Vue()
// in component A's method
bus.$emit('my-event', { foo: 'bar' })
// in component B, C, D, etc created hook
bus.$on('my-event', function (data) {
console.log(data)
})
https://vuejs.org/v2/guide/components.html#Non-Parent-Child-Communication
来源:https://stackoverflow.com/questions/46899901/with-vue-js-is-it-possible-to-listen-to-all-events-emitted-from-numerous-elemen