刘勇虎的官方网站
网站内容包含大前端、服务器开发、Python开发、iOS开发、Android开发、网站维护等技术文章。专注于分享技术经验,职业心得体会,IT优秀文章与教程创作。
Stay hungry,Stay foolish,Stay young
yarn add vuedraggable@next
npm i -S vuedraggable@next
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/3.0.2/vue.min.js"></script>
<!-- CDNJS :: Sortable (https://cdnjs.com/) -->
<script src="//cdn.jsdelivr.net/npm/sortablejs@1.10.2/Sortable.min.js"></script>
<!-- CDNJS :: Vue.Draggable (https://cdnjs.com/) -->
<script src="//cdnjs.cloudflare.com/ajax/libs/Vue.Draggable/4.0.0/vuedraggable.umd.min.js"></script>
<draggable
v-model="myArray"
group="people"
@start="drag=true"
@end="drag=false"
item-key="id">
<template #item="{element}">
<div>{{element.name}}</div>
</template>
</draggable>
import draggable from 'vuedraggable'
export default {
components: {
draggable,
},
data() {
return {
drag: false,
myArray: [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 3, name: 'Doe' }
]
}
}
}
<draggable v-model="myArray" tag="transition-group" item-key="id">
<template #item="{element}">
<div>{{element.name}}</div>
</template>
</draggable>
<draggable v-model="myArray" item-key="id">
<template #item="{element}">
<div>{{element.name}}</div>
</template>
<template #footer>
<button @click="addPeople">Add</button>
</template>
</draggable>
<draggable v-model="myArray" item-key="id">
<template #item="{element}">
<div>{{element.name}}</div>
</template>
<template #header>
<button @click="addPeople">Add</button>
</template>
</draggable>
<draggable v-model="myArray">
<div v-for="element in myArray" :key="element.id">{{element.name}}</div>
</draggable>
<draggable v-model="myArray" item-key="id">
<template #item="{element}">
<div>{{element.name}}</div>
</template>
</draggable>
代码:
<draggable
v-model="formData.contacts"
@start="drag = true"
@end="drag = false"
item-key="index"
>
<template #item="{ element, index }">
<!-- {{ element.name }} -->
<div class="connects">
<!-- {{ element.name }} -->
<el-input
v-model="element.name"
@input="inputContacts(index)"
placeholder="名字"
></el-input>
<el-input
v-model="element.phone"
@input="inputContacts(index)"
placeholder="联系方式"
></el-input>
</div>
</template>
</draggable>
删除中间注释。确保 #item
插槽只有一个根元素。
<draggable
v-model="formData.contacts"
@start="drag = true"
@end="drag = false"
item-key="index"
>
<template #item="{ element, index }">
<div class="connects">
<el-input
v-model="element.name"
@input="inputContacts(index)"
placeholder="名字"
></el-input>
<el-input
v-model="element.phone"
@input="inputContacts(index)"
placeholder="联系方式"
></el-input>
</div>
</template>
</draggable>
<template>
<div>
<draggable v-model="list1" group="people" @start="drag = true" @end="drag = false" item-key="id">
<template #item="{element}">
<div>{{element.name}}</div>
</template>
</draggable>
<draggable v-model="list2" group="people" @start="drag = true" @end="drag = false" item-key="id">
<template #item="{element}">
<div>{{element.name}}</div>
</template>
</draggable>
</div>
</template>
<script>
import draggable from 'vuedraggable'
export default {
components: {
draggable,
},
data() {
return {
drag: false,
list1: [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' }
],
list2: [
{ id: 3, name: 'Doe' },
{ id: 4, name: 'Smith' }
]
}
}
}
</script>