【发布时间】:2021-11-16 05:56:49
【问题描述】:
在 Vue3 中,我可以执行以下操作:
父组件:
<ChildComponent :props="{ id: 'the-id', class: 'the-class' }" />
子组件:
<template>
<input v-bind="props" />
</template>
<script>
export default {
props: {
props: { type: Object }
}
}
</script>
这将导致 HTML 如下所示:
<input id="the-id" class="the-class" />
我仍在学习 Vue,我想知道是否可以对事件侦听器/处理程序做同样的事情。
对于可重用组件,我可能需要不同的事件侦听器/处理程序,具体取决于我使用组件的位置。在一种形式中,我可能只需要子组件中的@input="...",在另一种形式中,我可能还需要@blur="...",或者我可能根本不需要事件监听器。
可以做类似的事情吗?
父组件:
<ChildComponent :events="{ input: function() { alert('input!'); }, blur: function() { alert('blur!'); } } />
子组件:
<template>
<input @events />
</template>
<script>
export default {
props: {
props: { type: Object }
}
}
</script>
谢谢;)
【问题讨论】:
标签: javascript vue.js vue-component vuejs3