我正在尝试使用Vue.js和vue-dragula框架构建一个具有可拖动元素的web应用程序。在应用程序中,我尝试使用多个容器,这些容器中的元素可以在容器中拖动。
在App.vue中
<template>
<div v-for="container in containers">
<container/>
</div>
</template>在Container.vue中
<template>
<div v-dragula="elements" bag="first-bag">
<div v-for="element in elements" :key="element.id">
<element v-bind="element"/>
</div>
</div>
</template>export default {
mounted() {
Vue.$dragula.$service.eventBus.$on('drop', () => {
console.log('Dropped');
});
}
}我正在尝试启用一个事件侦听器,该侦听器能够检测元素何时被删除。当当前的事件侦听器方法工作时,它会被多次调用。具体来说,它被称为容器数组的长度。例如,如果容器是一个长度为6的数组,那么'Dropped‘会被记录6次。如何才能使drop的事件侦听器只被调用一次?
发布于 2020-12-04 15:50:23
下面是我的解决方案。
App.vue
<template>
<div v-for="(container, containerIndex) in containers">
<container :containerIndex="containerIndex" />
</div>
</template>在Container.vue中
<template>
<div v-dragula="elements" :bag="'first-bag-'+containerIndex">
<div v-for="element in elements" :key="element.id">
<element v-bind="element"/>
</div>
</div>
</template>
<script>
export default {
props: ['containerIndex'],
mounted() {
Vue.$dragula.$service.eventBus.$on('drop', ([bag, curElmt, allElmts]) => {
if (bag == 'first-bag-'+this.containerIndex) {
console.log('Dropped'); // This line is not repate based on containers length
}
});
}
}
<script>https://stackoverflow.com/questions/59802141
复制相似问题