当前位置: 首页 > article >正文

记录一个拖拽组件vue3+ts

记录一个拖拽组件vue+ts

“vue”: “^3.0.0”
“typescript”: “~4.1.5”

我这个是vue 3的最高版本,可以使用defineModel
父组件

  <h1>props传值</h1>
  <ModuleOrder v-model:orderList="orderList" v-model:defaultList="defaultList" ></ModuleOrder>
  <button @click="setOrderData(1)" >新增一条</button>
  <button @click="setOrderData(2)" >删除一条</button><br>
  
  import ModuleOrder from './components/moduleOrder.vue'
  components:{ModuleOrder},
  // props传值
    const orderList:number[] = reactive([1,2,3,4,5])
    const defaultList:number[] = reactive([1,2,3,4,5])
    const setOrderData = (type:number)=>{
      if (type === 1){
        orderList.push(6)
        defaultList.push(6)
      }
      else{
        orderList.pop()
        defaultList.pop()
      }
    }

子组件moduleOrder.vue

<template>
    <button @click="reserData()" >重置</button>
  <ul>
    <li v-for="(item,index) in orderList" :key="item" draggable="true" 
    @dragstart="lDragStart(index)" @dragover.prevent @drop.prevent="lDrop(index)" >
        {{ item }}
    </li>
  </ul>
</template>

<script setup lang="ts" >
import {defineModel,onMounted,ref} from 'vue'
const defaultList = defineModel<number[]>("defaultList")
const orderList = defineModel<number[]>("orderList")
// const [defaultList,orderList] = defineModel<number[]>()
const startIndex = ref<number | null>(null)
const reserData = ()=>{
    orderList.value = defaultList.value
}
function lDragStart(index:number){
    startIndex.value = index
}
function lDrop(index:number){
    if (startIndex.value && orderList.value){
        const startItem:number = orderList.value[startIndex.value]
        orderList.value[startIndex.value] = orderList.value[index]
        orderList.value[index] = startItem
    }
}
</script>

可以用const [defaultList,orderList] = defineModel<number[]>()结构出变量,对于ts有个问题,<number[]>只能给第一个变量添加类型,第二个对象是Record

const defaultList: ModelRef<number[] | undefined, string>
const orderList: Record<string, true | undefined>

如果vue版本低于3.4,不支持defineModel就只能用defineProps和defineEmits

const props = defineProps(['orderList','defaultList'])
const emit = defineEmits(['update:orderList','update:defaultList'])

const orderList=ref([]);
const defaultList=ref([]);

watch([()=>props.orderList,()=>props.defaultList],()=>{
  orderList.value = props.orderList
  defaultList.value = props.defaultList
},{deep:true,immediate:true})

emit('update:LsitemList',orderList.value)
emit('update:defaultList',defaultList.value)


http://www.kler.cn/news/294847.html

相关文章:

  • 汇编:嵌入式软件架构学习资源
  • Python 算法交易实验88 QTV200日常推进-关于继续前进的思考
  • 爆改YOLOv8|利用MobileNetV4 的UIB改进C2f模块-yolov8改进
  • 【0324】Postgres内核 Shared Buffer Access Rules (共享缓冲区访问规则)说明
  • 数据结构代码集训day15(适合考研、自学、期末和专升本)
  • GraphPad Prism 10 for Mac/Win:高效统计分析与精美绘图的科学利器
  • 【Qt】文件对话框QFileDialog
  • 设计模式大全和详解,含Python代码例子
  • 基于“SRP模型+”多技术融合在生态环境脆弱性评价模型构建、时空格局演变分析与RSEI 指数的生态质量评价及拓展应用
  • 编写vue的输入框的自定义指令研究
  • 力扣9.7
  • 最新版 Java 网络编程经典案例:IM 系统、网络拷贝|万字笔记
  • 软件工程-图书管理系统的概要设计
  • 网络层ip协议
  • echarts 水平柱图 科技风
  • 单北斗新时代,遨游通讯四款防爆手机筑牢安全防线
  • Java数组(详解版)
  • Windows .NET8 实现 远程一键部署,几秒完成发布,提高效率 - CICD
  • Rust : 从事量化的生态现状与前景
  • 漫谈设计模式 [17]:状态模式
  • 调研-libevent
  • VitePress 自定义 CSS 指南
  • docker基础命令总结
  • 流程图符号速查:快速掌握流程图绘制要点
  • Kafka【十二】消费者拉取主题分区的分配策略
  • NISP 一级 —— 考证笔记合集
  • RISC-V (十二)系统调用
  • python网络爬虫(五)——爬取天气预报
  • 风趣图解LLMs RAG的15种设计模式-第一课
  • 自然语言处理系列六十二》神经网络算法》MLP多层感知机算法