Vue3+Element+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>>()

到此這篇關於Vue3+Element+Ts實現表單的基礎搜索重置等功能的文章就介紹到這瞭,更多相關Element Ts表單搜索重置內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: