首页 > 其他分享 >Vue3初学之Element-plus Form表单

Vue3初学之Element-plus Form表单

时间:2025-01-17 15:29:42浏览次数:3  
标签:const string Form Please trigger plus Vue3 message true

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-checkbox
      label="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-picker
      v-model="value2"
      arrow-control
      placeholder="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-form
    ref="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-v2
        v-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-picker
            v-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-picker
            v-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: string
  region: string
  count: string
  date1: string
  date2: string
  delivery: boolean
  location: string
  type: string[]
  resource: string
  desc: 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) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}))
</script>

标签:const,string,Form,Please,trigger,plus,Vue3,message,true
From: https://blog.csdn.net/fmdpenny/article/details/145189498

相关文章

  • 实施零信任模型的方法指南 支柱1用户 集成ICAM平台(Integrated ICAM Platform)
    1.9.1功能描述国防部各组织和整个企业采用企业级身份管理和公钥基础设施(PKI)系统,用于跟踪整个网络中的用户、管理员和非人员实体(NPE)的身份,并确保访问权限仅限于有需要且有知情权的人员,各组织可以通过凭证管理系统、身份治理和管理工具以及访问管理工具来验证其是否为必需......
  • 关于vue3 路由离开前 更新pinia 导致页面watch被触发 解决方法
    背景:在vue中,使用watch监听pinia中的数据是否变化来调用apiwatch(()=>{return[pinia.data,]},(newV,oldV)=>{axios.get('a.b',params).then((response)=>{........});},{immediate:true})在监听piniadata的时候,如......
  • transition、animation、transform三者有什么区别?
    在前端开发中,transition、animation和transform是三个常用于实现动画效果的CSS属性。它们各自有不同的特点和用途,下面将分别进行解释和比较。transform作用:transform属性主要用于元素的2D或3D转换,可以实现旋转、缩放、移动和倾斜等效果。使用方式:通过指定不同的转换函数(如tr......
  • webpack5 从零搭建 vue3 项目
    目前vue3官网推荐的工具链已经是vite了,就算是要使用webpack甚至是webpack5,也可以直接使用vue-cli。然而之所以写这个,可以当是webpack5的一个学习文章。同时也是因为之前有个项目是在vue3刚出来的时候使用vue-cli(那是官网还是推荐使用webpack,以及但是得版本为webp......
  • Vue3在线聊天室
    更多开源项目请关注我的gitee:乌鸦像写字台(关注公众号:寻川的AI工具库免费得毕设必备软件以及详细项目运行文档)(he-haoran-hhh)-Gitee.com在Layout.vue中添加菜单选项<el-menu-itemindex="/home">首页</el-menu-item><el-menu-itemindex="/im">天农聊天室</el-menu-item......
  • MindIE+MindFormers推理方案指导
    目录组件介绍CANNMindIEMindFormers版本配套与安装指导版本配套关系安装指导使用指导组件介绍CANNCANN是什么异构计算架构CANN(ComputeArchitectureforNeuralNetworks)是昇腾针对AI场景推出的异构计算架构,向上支持多种AI框架,包括MindSpore、PyTorch、TensorFlow等,向下服务A......
  • 基SpringBoot+Vue3的游乐园管理系统
    文章目录详细视频演示项目介绍技术介绍功能介绍核心代码系统效果图详细视频演示文章底部名片,获取项目的完整演示视频,免费解答技术疑问项目介绍  游乐园作为娱乐休闲的重要场所,其管理效率和服务质量直接影响到游客的满意度和游乐园的声誉。传统的游乐园管理方式......
  • vue3 实现标签拖拽排序 + curd
    ......
  • 市面上唯一一本全面解析Transformer的书《Transformer、BERT、GPT 大语言模型原理深度
    Transformer,BERT,andGPT:IncludingChatGPTandPromptEngineering,出版于2023年11月,作者是奥斯瓦尔德·坎佩萨托(OswaldCampesato)奥斯瓦尔德·坎佩萨托(OswaldCampesato):专门研究深度学习、Java、Android和TensorFlow。他是25本书的作者/合著者,其中包括TensorF......
  • (14-3-02)基于Latent Diffusion Transformer的文生视频系统:数据集处理(2)加载并处理Taic
    6.4.3 加载并处理Taichi数据集文件taichi_datasets.py实现了一个Taichi数据集类,用于加载和处理分帧存储的视频数据,特别是太极表演相关的帧序列。它包括从数据目录中读取视频帧、按时间进行帧采样、将帧数据转换为张量并应用数据增强等功能。代码通过torch.utils.data.Da......