Vue3初学之Element-plus Form表单

server/2025/1/21 1:55:58/

1.使用 el-form 组件
el-form 是一个表单容器,可以包含多个 el-form-item,每个 el-form-item 包裹具体的表单控件,如输入框、选择器、日期选择器等。

<template><el-form :model="form" label-width="120px"><el-form-item label="用户名"><el-input v-model="form.username"></el-input></el-form-item><el-form-item label="密码"><el-input v-model="form.password" type="password"></el-input></el-form-item><el-checkbox-group v-model="checkList"><el-checkbox label="Option A" value="Value A" /><el-checkbox label="Option B" value="Value B" /><el-checkbox label="Option C" value="Value C" /><el-checkbox label="disabled" value="Value disabled" disabled /><el-checkboxlabel="selected and disabled"value="Value selected and disabled"disabled/></el-checkbox-group><div class="example-basic"><el-time-picker v-model="value1" placeholder="Arbitrary time" /><el-time-pickerv-model="value2"arrow-controlplaceholder="Arbitrary time"/></div><el-form-item><el-button type="primary" @click="submitForm">提交</el-button></el-form-item></el-form>
</template><script setup>
import { ref } from 'vue';
const checkList = ref(['Value selected and disabled', 'Value A'])
const form = ref({username: '',password: ''
});const submitForm = () => {console.log('提交表单数据:', form.value);console.log('多选',checkList.value,value1,value2);
};
const value1 = ref()
const value2 = ref()
</script><style>
.example-basic .el-date-editor {margin: 8px;
}
</style>

在这里插入图片描述
2.Transter
比较使用的功能,可以跟官网学习
在这里插入图片描述

  1. 表单校验
    该功能也是非常使用的功能,用于学习
<template><el-formref="ruleFormRef"style="max-width: 600px":model="ruleForm":rules="rules"label-width="auto"class="demo-ruleForm":size="formSize"status-icon><el-form-item label="Activity name" prop="name"><el-input v-model="ruleForm.name" /></el-form-item><el-form-item label="Activity zone" prop="region"><el-select v-model="ruleForm.region" placeholder="Activity zone"><el-option label="Zone one" value="shanghai" /><el-option label="Zone two" value="beijing" /></el-select></el-form-item><el-form-item label="Activity count" prop="count"><el-select-v2v-model="ruleForm.count"placeholder="Activity count":options="options"/></el-form-item><el-form-item label="Activity time" required><el-col :span="11"><el-form-item prop="date1"><el-date-pickerv-model="ruleForm.date1"type="date"aria-label="Pick a date"placeholder="Pick a date"style="width: 100%"/></el-form-item></el-col><el-col class="text-center" :span="2"><span class="text-gray-500">-</span></el-col><el-col :span="11"><el-form-item prop="date2"><el-time-pickerv-model="ruleForm.date2"aria-label="Pick a time"placeholder="Pick a time"style="width: 100%"/></el-form-item></el-col></el-form-item><el-form-item label="Instant delivery" prop="delivery"><el-switch v-model="ruleForm.delivery" /></el-form-item><el-form-item label="Activity location" prop="location"><el-segmented v-model="ruleForm.location" :options="locationOptions" /></el-form-item><el-form-item label="Activity type" prop="type"><el-checkbox-group v-model="ruleForm.type"><el-checkbox value="Online activities" name="type">Online activities</el-checkbox><el-checkbox value="Promotion activities" name="type">Promotion activities</el-checkbox><el-checkbox value="Offline activities" name="type">Offline activities</el-checkbox><el-checkbox value="Simple brand exposure" name="type">Simple brand exposure</el-checkbox></el-checkbox-group></el-form-item><el-form-item label="Resources" prop="resource"><el-radio-group v-model="ruleForm.resource"><el-radio value="Sponsorship">Sponsorship</el-radio><el-radio value="Venue">Venue</el-radio></el-radio-group></el-form-item><el-form-item label="Activity form" prop="desc"><el-input v-model="ruleForm.desc" type="textarea" /></el-form-item><el-form-item><el-button type="primary" @click="submitForm(ruleFormRef)">Create</el-button><el-button @click="resetForm(ruleFormRef)">Reset</el-button></el-form-item></el-form>
</template><script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'interface RuleForm {name: stringregion: stringcount: stringdate1: stringdate2: stringdelivery: booleanlocation: stringtype: string[]resource: stringdesc: string
}const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({name: 'Hello',region: '',count: '',date1: '',date2: '',delivery: false,location: '',type: [],resource: '',desc: '',
})const locationOptions = ['Home', 'Company', 'School']const rules = reactive<FormRules<RuleForm>>({name: [{ required: true, message: 'Please input Activity name', trigger: 'blur' },{ min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },],region: [{required: true,message: 'Please select Activity zone',trigger: 'change',},],count: [{required: true,message: 'Please select Activity count',trigger: 'change',},],date1: [{type: 'date',required: true,message: 'Please pick a date',trigger: 'change',},],date2: [{type: 'date',required: true,message: 'Please pick a time',trigger: 'change',},],location: [{required: true,message: 'Please select a location',trigger: 'change',},],type: [{type: 'array',required: true,message: 'Please select at least one activity type',trigger: 'change',},],resource: [{required: true,message: 'Please select activity resource',trigger: 'change',},],desc: [{ required: true, message: 'Please input activity form', trigger: 'blur' },],
})const submitForm = async (formEl: FormInstance | undefined) => {if (!formEl) returnawait formEl.validate((valid, fields) => {if (valid) {console.log('submit!')} else {console.log('error submit!', fields)}})
}const resetForm = (formEl: FormInstance | undefined) => {if (!formEl) returnformEl.resetFields()
}const options = Array.from({ length: 10000 }).map((_, idx) => ({value: `${idx + 1}`,label: `${idx + 1}`,
}))
</script>

http://www.ppmy.cn/server/160048.html

相关文章

PyTorch使用教程(2)-torch包

1、简介 torch包是PyTorch框架最外层的包&#xff0c;主要是包含了张量的创建和基本操作、随机数生成器、序列化、局部梯度操作的上下文管理器等等&#xff0c;内容很多。我们基础学习的时候&#xff0c;只有关注张量的创建、序列化&#xff0c;随机数、张量的数学数学计算等常…

C语言笔记——第二章 顺序结构程序设计(三)

2.3 数据输出 一、字符型输出函数&#xff08;putchar&#xff09; 1、函数&#xff1a;putchar(c) 参数&#xff1a;c为字符常量&#xff0c;变量或表达式 功能&#xff1a;把参数c输出到显示器上 2、当c为被一个单引号引起的字符时&#xff0c;则输出该字符 #include&l…

AAPM:基于大型语言模型代理的资产定价模型,夏普比率提高9.6%

“AAPM: Large Language Model Agent-based Asset Pricing Models” 论文地址&#xff1a;https://arxiv.org/pdf/2409.17266v1 Github地址&#xff1a;https://github.com/chengjunyan1/AAPM 摘要 这篇文章介绍了一种利用LLM代理的资产定价模型&#xff08;AAPM&#xff09;…

深入了解卷积神经网络(CNN):图像处理与深度学习的革命性技术

深入了解卷积神经网络&#xff08;CNN&#xff09;&#xff1a;图像处理与深度学习的革命性技术 导语 卷积神经网络&#xff08;CNN&#xff09;是现代深度学习领域中最重要的模型之一&#xff0c;特别在计算机视觉&#xff08;CV&#xff09;领域具有革命性的影响。无论是图…

Redisson发布订阅学习

介绍 Redisson 的消息订阅功能遵循 Redis 的发布/订阅模式&#xff0c;该模式包括以下几个核心概念&#xff1a; 发布者&#xff08;Publisher&#xff09;&#xff1a;发送消息到特定频道的客户端。在 Redis 中&#xff0c;这通过 PUBLISH 命令实现。 订阅者&#xff08;Sub…

SAP POC 项目完工进度 - 收入确认方式【工程制造行业】【新准则下工程项目收入确认】

1. SAP POC收入确认基础概念 1.1 定义与原则 SAP POC&#xff08;Percentage of Completion&#xff09;收入确认方式是一种基于项目完工进度来确认收入的方法。其核心原则是根据项目实际完成的工作量或成本投入占预计总工作量或总成本的比例&#xff0c;来确定当期应确认的收…

Delete `␍`eslintprettier/prettier

npm vite build报上面错&#xff0c;开发网上查资料 知道这是因为回车符号的问题&#xff1a;linux window对文件行尾的定义不同所致 解决方案也简单&#xff0c;就是配置endOfLine参数为 "auto"。 配置时傻眼了&#xff0c;网上说是.prettierrc文件&#xff0c;但…

【gin】中间件使用之jwt身份认证和Cors跨域,go案例

Gin-3 中间件编程及 JWT 身份认证 1. Gin 中间件概述 中间件是处理 HTTP 请求的函数&#xff0c;可以在请求到达路由处理函数之前或之后对请求进行处理。 在 Gin 框架中&#xff0c;中间件常用于处理日志记录、身份验证、权限控制等功能。 router : gin.Default() router.Us…