1. 程式人生 > 程式設計 >Vue3+Element+Ts實現表單的基礎搜尋重置等功能

Vue3+Element+Ts實現表單的基礎搜尋重置等功能

從2的寫法轉變為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,grade)
}
// 重置
const submitReset = () => {
  myform.value?.resetFields()
}
</script&
gt; <template> <div class="mysearch"> <el-form :model="formData" label-width="80px" ref="myform"> <el-rwww.cppcns.comow :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-optiowww.cppcns.com
n 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"
        http://www.cppcns.com    >查 詢</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: '',grade: ''
    })
    const myform = ref<InstanceType<typeof ElForm>>()
    // 查詢
    const submitForm = () => {
      const { name,grade } = formData
      console.log(name,grade)
    }
    // 重置
    const submitReset = () => {
      myform.value?.resetFields()
    }
    return {
      formData,myform,submitForm,submitReset
    }
  }
})
</script>

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

區別:

  • 寫法一由上到下,分別是、HTML、,類似於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表單搜尋重置內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!