Vue 中是否有可能以与此类似的方式处理一个函数内的传入事件?
<template>
<component v-on="eventsDistributor(eventName, $event)"/>
</template>
<script>
export default {
props: {
handlers: Object,
},
methods : {
eventsDistributor (name, $event) {
let handler = this.handlers[name]
if (handler) return handler($event)
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
ton*_*y19 25
我想$listeners可能是你需要的。它是一个包含所有父级侦听器的对象,并且可以通过v-on="$listeners".
例如,您有一个<button>包装器组件,并且希望包装器上的所有侦听器都绑定到按钮:
<!-- MyButtonWrapper.vue -->
<template>
<button v-on="$listeners">Click</button>
</template>
<!-- Parent.vue -->
<template>
<!-- click and mouseover listeners are bound to inner button -->
<MyButtonWrapper @click="onClick" @mouseover="@mouseover" />
</template>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6557 次 |
| 最近记录: |