您好,登錄后才能下訂單哦!
本篇內容主要講解“Vue3+Element+Ts如何實現表單的基礎搜索重置等功能”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Vue3+Element+Ts如何實現表單的基礎搜索重置等功能”吧!
寫法一(推薦):
<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如何實現表單的基礎搜索重置等功能”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。