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

vue3父组件控制子组件表单验证及获取子组件数值方法

1、关键部分的代码如下,我努力交代清楚了,希望能让大家看懂。

<template><KeepAlive><component ref="comp" :is="compNames[steps[compIndex].comp]" /></KeepAlive><el-button @click="prevBtn" v-show="compIndex">上一步</el-button><el-button type="primary" @click="nextBtn">{{compIndex ? '提交': '下一步'}}</el-button>
</template><script setup lang="ts">
//引入两个子组件
import onefrom './onefrom.vue'
import twoForm from './twoForm.vue'//子组件切换相关参数
const steps = [{title:'111',comp:'one',ref:'oneForm'},{title:'222',comp:'two',ref:'twoForm'}]
//当前组件索引
const compIndex = ref(0)//子组件名
type compName = {[key:string]:any
}
const compNames = shallowReactive<compName>({oneForm,twoForm})//组件设置ref="comp"
const comp = ref(null);  //点击按钮验证子组件表单
const prevBtn = () => {compIndex.value=0
}
const nextBtn = () => {if (compIndex.value == 0 && comp.value.$refs.formRef) {comp.value.$refs.formRef.validate((valid) => {if (valid) {compIndex.value = 1  //表单验证通过后切换到子组件twoForm}});}
}

2、顺便记录下父组件获取子组件数值的写法,和获取当前日期的函数。

子组件代码

<template><el-form-item label="创建时间"><el-date-pickerv-model="currentDate"type="date"/></el-form-item>
</template><script setup lang="ts">
//获取当前日期
let currentDate = computed<string>(() => {let currentDate = new Date();let year = currentDate.getFullYear();let month = currentDate.getMonth() + 1;let day = currentDate.getDate();return year + '-' + month + '-' + day;
});// 表单参数
const initFormData = reactive<formulaForm>({id: null,name: undefined,createTime: currentDate.value
})//将表单参数暴露给父组件
defineExpose({initFormData
})
</script>

父组件接收参数

</template><component ref="comp" :is="compNames[steps[compIndex].comp]" /><el-button @click="Btn">获取参数</el-button>
</template><script setup lang="ts">
const Btn = () => {console.log(comp.value.initFormData)
}
</script>
http://www.lryc.cn/news/468741.html

相关文章:

  • 【JavaEE】【多线程】单例模式
  • Java.6--多态-设计模式-抽象父类-抽象方法
  • JAVA Maven 的安装与配置
  • 【程序分享】PCB元件坐标对齐工具 V1.3
  • [bug] vllm 0.6.1 RuntimeError: operator torchvision::nms does not exist
  • 处理Hutool的Http工具上传大文件报OOM
  • transforms的使用
  • python-PyQt项目实战案例:制作一个视频播放器
  • 反向传播的微积分原理 | Chapter 4 | Deep Learning | 3Blue1Brown
  • matlab读取excel表格
  • 基于springboot+vue实现的助学兼职系统(源码+L文+ppt)4-092
  • ⌈ 传知代码 ⌋ 农作物病害分类(Web端实现)
  • CMU生成式人工智能大模型:从入门到放弃(九)
  • HTML基础总结
  • EXCELL中如何两条线画入一张图中,标记坐标轴标题?
  • Zabbix企业级分布式监控环境部署
  • 水轮发电机油压自动化控制系统解决方案介绍
  • 今天不分享技术,分享秋天的故事
  • 转录组上游分析流程(三)
  • excel判断某一列(A列)中的数据是否在另一列(B列)中
  • [环境配置]macOS上怎么查看vscode的commit id
  • .net framework 3.5sp1组件安装进度条不动启动错误怎么解决
  • 学习threejs,利用THREE.ExtrudeGeometry拉伸几何体实现svg的拉伸
  • 大模型之三十二-语音合成TTS(coqui) 之二 fine-tune
  • JVM的内存模型是什么,每个区域的作用是什么,以及面试题(含答案)
  • 《设计模式三》Java代理模式实现
  • vue3中计算属性的用法以及使用场景
  • pytorh学习笔记——cifar10(六)MobileNet V1网络结构
  • 报表系统-连接数据库操作
  • [计算机网络] 常见端口号