为什么 '@drop' 事件在 vue 中对我不起作用?

mk_*_*_xt 10 drag-and-drop dom-events vue.js vue-events drop

@drop监听器不会为我工作。它不会调用我告诉它调用的方法。

我想拖动芯片并能够将其放在另一个组件上,并执行一个功能,但是在放下芯片的时候,dropLink方法没有执行,所以我假设@drop事件没有发出。

控制台上不会显示任何错误。

其余事件运行良好,例如@dragstart.

这是我使用的组件的代码:

<template>
  <div
    @keydown="preventKey"
    @drop="dropLink"
  >
    <template
      v-if="!article.isIndex"
    >
      <v-tooltip bottom>
        <template v-slot:activator="{ on }">
          <v-chip
            small
            draggable
            class="float-left mt-2"
            v-on="on"
            @dragstart="dragChipToLinkToAnotherElement"
          >
            <v-icon x-small>
              mdi-vector-link
            </v-icon>
          </v-chip>
        </template>
        <span>Link</span>
      </v-tooltip>

      <v-chip-group
        class="mb-n2"
        show-arrows
      >
        <v-chip
          v-for="(lk, index) in links"
          :key="index"
          small
          outlined
          :class="{'ml-auto': index === 0}"
        >
          {{ lk.text }}
        </v-chip>
      </v-chip-group>
    </template>

    <div
      :id="article.id"
      spellcheck="false"
      @mouseup="mouseUp($event, article)"
      v-html="article.con"
    />
  </div>
</template>

<script>
export default {
  name: 'ItemArticle',
  props: {
    article: {
      type: Object,
      required: true
    }
  },
  computed: {
    links () {
      return this.article.links
    }
  },
  methods: {
    mouseUp (event, article) {
      this.$emit('mouseUp', { event, article })
    },
    preventKey (keydown) {
      this.$emit('preventKey', keydown)
    },
    dragChipToLinkToAnotherElement (event) {
      event.dataTransfer.setData('text/plain', this.article.id)
    },
    dropLink (e) {
      //but this method is never called
      console.log('evento drop is ok', e)
    }
  }
}
</script>
Run Code Online (Sandbox Code Playgroud)

在项目中,我也在使用 Nuxt,以防万一。

ton*_*y19 19

为了使diva 放置目标,必须取消div'sdragenterdragoverevents。您可以Event.preventDefault()使用.prevent 事件修饰符调用这些事件:

<div @drop="dropLink" @dragenter.prevent @dragover.prevent></div>
Run Code Online (Sandbox Code Playgroud)

如果您需要根据拖动数据类型接受/拒绝放置,请设置一个有条件地调用的处理程序Event.preventDefault()

<div @drop="dropLink" @dragenter="checkDrop" @dragover="checkDrop"></div>
Run Code Online (Sandbox Code Playgroud)
export default {
  methods: {
    checkDrop(e) {
      if (/* allowed data type */) {
        e.preventDefault()
      }
    },
  }
}
Run Code Online (Sandbox Code Playgroud)

演示