vue3使用vuedraggable實現拖拽功能
更新時間:2022年04月06日 10:08:29 作者:smily_word
這篇文章主要為大家詳細介紹了vue3使用vuedraggable實現拖拽功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue3使用vuedraggable實現拖拽功能的具體代碼,供大家參考,具體內容如下
1、npm i vuedraggable -S,使用這個命令,vue3會報錯,如下圖
2、使用npm uninstall vuedraggable -S 卸載,再使用npm i -S vuedraggable@next下載最新版
3、vue使用代碼如下:
<template> ? <div> ? ? <h1 class="title">拖拽</h1> ? ? <draggable ? ? ? class="wrapper" ? ? ? v-model="list" ? ? ? @start="drag = true" ? ? ? @end="drag = false" ? ? ? item-key="index" ? ? > ? ? ? <template #item="{ element }"> ? ? ? ? <div class="item"> ? ? ? ? ? <p>{{ element }}</p> ? ? ? ? </div> ? ? ? </template> ? ? </draggable> ? </div> </template> ? <script> import { reactive, toRefs, onMounted } from 'vue' import draggable from 'vuedraggable' ? export default { ? name: 'dragAndDrop', ? components: { draggable }, ? setup () { ? ? const state = reactive({ ? ? ? drag: false, ? ? ? list: [1, 2, 3, 4, 5, 6] ? ? }) ? ? onMounted(() => {}) ? ? return { ? ? ? ...toRefs(state) ? ? } ? } } </script> ? <style scoped> .title { ? text-align: center; ? color: #42b983; } .wrapper { ? display: flex; ? justify-content: center; ? width: 100%; } .item { ? width: 100px; ? height: 100px; ? font-size: 50px; ? text-align: center; ? line-height: 100px; ? margin: 10px; ? background-color: #42b983; ? color: #ffffff; } </style>
4、效果如下圖
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Vue3中的setup語法糖、computed函數、watch函數詳解
這篇文章主要介紹了Vue3中的setup語法糖、computed函數、watch函數,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03