Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drag and drop in vue js without component

I want to use html 5 drag and drop in vue js .

I see the w3schools tutorial about drag and drop . It works in a simple html file but doesn't work in my vue project

My tutorials code and link is : w3schools - drag : https://www.w3schools.com/jsref/event_ondrag.asp and my error says : Uncaught ReferenceError: allowDrop is not defined

I define all methods in method scope in vue js.

like image 392
mostafa faryabi Avatar asked May 16 '19 11:05

mostafa faryabi


People also ask

How do I drag and drop in VueJS?

Initially, we need to set the draggable attribute as true to enable the Drag and Drop API for the span element that is around our slot . It's important to mention that, in this case, even though we're working with VueJS, we have to set the value "true" explicitly, otherwise it won't work as expected.

How do I make my Vue component invisible?

To hide the element but keep its space use :class="{ invisible: ! value }" to assign invisible class (which has visibility: hidden style applied to it).

How do I reinitialize component Vue?

The best way to force Vue to re-render a component is to set a :key on the component. When you need the component to be re-rendered, you just change the value of the key and Vue will re-render the component.

Does Vue use component?

It's common for an app to be organized into a tree of nested components: This is very similar to how we nest native HTML elements, but Vue implements its own component model that allow us to encapsulate custom content and logic in each component.


1 Answers

You can use @dragover.prevent along with @drop.stop.prevent to prevent web browsers default behavior (like opening the files).

You can go and check the documentation about events handling if you want more details : VueJS Event Handling Documentation

Here is an example of how you could implement a basic drag & drop :

new Vue({
  el: "#app",
  methods: {
    // Will be fired by our '@drop.stop.prevent'; in this case, when a file is dropped over our app
    onDrop(event) {
      const file = event.dataTransfer.files[0];

      // Do something with the dropped file
      console.log(file)
    }
  }
})
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}

p {
  text-align: center
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js"></script>

<div id="app" @dragover.prevent @drop.stop.prevent="onDrop">
  <p>Drag & Drop</p>
</div>
like image 73
Cédric M. Avatar answered Oct 18 '22 16:10

Cédric M.