Vue3+Element Plue+Ts实现表单的基础搜索、重置等功能

从Vue2的写法转变为Vue3的格式之后,会有一些写法和代码结构的改变,这里对一些重点进行介绍。

在这里插入图片描述

代码结构:

写法一(推荐):
<script setup lang="ts">
import { ref, reactive } from 'vue'
import type { ElForm } from 'element-plus'
const myform = ref<InstanceType<typeof ElForm>>()
const formData = reactive({
  name: '',
  subject: '',
  grade: ''
})
// 查找
const submitForm = () => {
  const { name, subject, grade } = formData
  console.log(name, subject, grade)
}
// 重置
const submitReset = () => {
  myform.value?.resetFields()
}
</script>

<template>
  <div class="mysearch">
    <el-form :model="formData" label-width="80px" ref="myform">
      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item label="名称" prop="name">
            <el-input v-model="formData.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="学科" prop="subject">
            <el-input v-model="formData.subject"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="年级" prop="grade">
            <el-select v-model="formData.grade" placeholder="请选择">
              <el-option label="一年级" value="shanghai"></el-option>
              <el-option label="二年级" value="beijing"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="2" :offset="19">
          <el-button type="primary" size="medium" @click="submitForm">
            查 询
          </el-button>
        </el-col>
        <el-col :span="2" :offset="0">
          <el-button type="primary" size="medium" @click="submitReset">
            重 置
          </el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<style scoped lang="less">
.mysearch {
  padding: 20px;
}
</style>
写法二:
<template>
  <div class="mysearch">
    <el-form ref="myform" :model="formData" label-width="80px">
      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item label="名称" prop="name">
            <el-input v-model="formData.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="学科" prop="subject">
            <el-input v-model="formData.subject"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="年级" prop="grade">
            <el-select v-model="formData.grade" placeholder="请选择">
              <el-option label="一年级" value="shanghai"></el-option>
              <el-option label="二年级" value="beijing"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="2" :offset="19">
          <el-button type="primary" size="medium" @click="submitForm"
            >查 询</el-button
          >
        </el-col>
        <el-col :span="2" :offset="0">
          <el-button type="primary" size="medium" @click="submitReset"
            >重 置</el-button
          >
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script lang="ts">
import { defineComponent, reactive, ref } from 'vue'
import { ElForm } from 'element-plus'

export default defineComponent({
  setup() {
    const formData = reactive({
      name: '',
      subject: '',
      grade: ''
    })
    const myform = ref<InstanceType<typeof ElForm>>()
    // 查找
    const submitForm = () => {
      const { name, subject, grade } = formData
      console.log(name, subject, grade)
    }
    // 重置
    const submitReset = () => {
      myform.value?.resetFields()
    }
    return {
      formData,
      myform,
      submitForm,
      submitReset
    }
  }
})
</script>

<style scoped lang="less">
.mysearch {
  padding: 20px;
}
</style>

区别:

  • 写法一由上到下,分别是JS、HTML、Css,类似于React的写法,逻辑也清晰
  • 写法一由上到下,分别是HTML、JS、Css,和之前的Vue2写法类似
  • 写法一的格式不需要将data,methods等内容进行导出,节省了代码量

重点:

1、Element Plus的官网Demo的代码里面没有写prop

  <el-form-item label="Activity name">
      <el-input v-model="form.name"></el-input>
    </el-form-item>

为了实现数据的响应式,在写的时候需要自己绑定
2、使用el-form的ref需要引入ElForm

import { ElForm } from 'element-plus'
const myform = ref<InstanceType<typeof ElForm>>()

如有问题,可以评论或私信我。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值