search
HomeWeb Front-endVue.jsHow Vue+ElementUI handles oversized forms

How Vue+ElementUI handles oversized forms

Nov 22, 2021 pm 07:49 PM
elementuivue

This article will take you through how Vue ElementUI handles very large forms. I hope it will be helpful to you!

How Vue+ElementUI handles oversized forms

Due to recent business adjustments in the company, the logic of the previous long form has changed a lot, so I plan to reconstruct it (it was written by a background manager who has resigned. , and there are no comments. One component has 4000 lines written, which is really powerless). For your convenience, I have split the project into 14 components and simplified them.

Overall idea

  • Large forms are split according to business modules
  • Use validate# provided by el-form when saving ##Method for verification (Loop to verify each split component)
  • mixin public extraction of each component (also conducive to the maintenance of later projects)
[Related recommendation: "

vue.js Tutorial"]

Start

    Here we use
  • to split 2 componentsFor example: form1, form2 (for the convenience of readers, do not use names)
  • Why are the two components here
  • ref and model tied together? The determined ones are all form will be explained later (for the convenience of later maintenance)
  • // form1 组件
    <template>
        <el-form
          ref="form"
          :model="form"
          label-width="10px"
        >
          <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" />
          </el-form-item>
        </el-form>
    </template>
    <script>
    export default {
      name: &#39;Form1&#39;,
        props: {
          form: {}
      },
      data() {
        return {
          rules: {
            name: [
              { required: true, message: &#39;请输入姓名&#39;, trigger: &#39;blur&#39; }
            ]
          }
        }
      },
      methods: {
        // 这里是是为了父组件循环调用校验
        validForm() {
            let result = false
            this.$refs.form.validate(valid => valid && (result = true))
            return result
        }
        // 我这里还用了另一种方式写的,但是循环校验的时候是promise对象,有问题,望大佬们指点一二
        validForm() {
            // 明明这里输出的结构是 Boolean 值,但在父组件循环调用之后就是promise类型,需要转换一下才行
            return this.$refs.form.validate().catch(e => console.log(e))
        }
      }
    }
    </script>
    
    // form2 组件
    <template>
        <el-form
          ref="form"
          :model="form"
          label-width="10px"
        >
          <el-form-item label="年龄" prop="age">
              <el-input v-model="form.age" />
          </el-form-item>
        </el-form>
    </template>
    <script>
    export default {
      name: &#39;Form2&#39;,
      props: {
          form: {}
      },
      data() {
        return {
          rules: {
            name: [
              { required: true, message: &#39;请输入年龄&#39;, trigger: &#39;blur&#39; }
            ]
          }
        }
      },
      methods: {
        // 这里是是为了父组件循环调用校验
        validForm() {
            let result = false
            this.$refs.form.validate(valid => valid && (result = true))
            return result
        }
      }
    }
    </script>
    Look at how the parent component is referenced
  • // 父组件
    <template>
        <div class="parent">
            <form1 ref="form1" :form="formData.form1" />
            <form2 ref="form2" :form="formData.form2" />
            <el-button type="primary" @click="save">报错</el-button>
        </div>
    </template>
    <script>
    ... 省略引用
    export default {
        name: &#39;parent&#39;,
        ... 省略注册
        data () {
            return {
                formData: {
                    form1: {},
                    form2: {}
                }
            }
        },
    }
    </script>
    Since the attribute names
  • form1 and form2 in formData are used on the ref of the subform component respectively, they can be Find them in turn during traversal and modify the save function. The code is as follows:
  • methods: {
        save () {
            // 每个表单对象的 key 值,也就是每个表单的 ref 值
            const formKeys = Object.keys(this.formData)
            // 执行每个表单的校验方法
            const valids = formKeys.map(item => this.$refs[item].validForm())
            // 所有表单通过检验之后的逻辑
            if (valids.every(item => item)) {
              console.log(11)
            }
        }
    }
Answer why the two components

ref and model are bound to form

    By comparison we can find that
  • form1 form2 has common props methods
  • We extract it through mixin
  • export default {
      props: {
        form: {
          required: true,
          type: Object,
          default: () => {}
        },
      },
      methods: {
        validForm () {
          let result = false
          this.$refs.form.validate(valid => valid && (result = true))
          return result
        }
      }
    }
    Reference the
  • minix in form1 form2 and delete the corresponding properties and methods in the corresponding component
End

    It is very troublesome to solve the extremely large form. Here we only split the components.
  • The linkage between components is also a big difficulty. We will send it out after finishing it next time.
  • You are welcome to discuss how you solved it in the comment area, and you are also welcome to point out the shortcomings to the author
For more programming-related knowledge, please visit:

Introduction to Programming! !

The above is the detailed content of How Vue+ElementUI handles oversized forms. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete
What Happens When the Vue.js Virtual DOM Detects a Change?What Happens When the Vue.js Virtual DOM Detects a Change?May 14, 2025 am 12:12 AM

WhentheVue.jsVirtualDOMdetectsachange,itupdatestheVirtualDOM,diffsit,andappliesminimalchangestotherealDOM.ThisprocessensureshighperformancebyavoidingunnecessaryDOMmanipulations.

How Accurate Is It to Think of Vue.js's Virtual DOM as a Mirror of the Real DOM?How Accurate Is It to Think of Vue.js's Virtual DOM as a Mirror of the Real DOM?May 13, 2025 pm 04:05 PM

Vue.js' VirtualDOM is both a mirror of the real DOM, and not exactly. 1. Create and update: Vue.js creates a VirtualDOM tree based on component definitions, and updates VirtualDOM first when the state changes. 2. Differences and patching: Comparison of old and new VirtualDOMs through diff operations, and apply only the minimum changes to the real DOM. 3. Efficiency: VirtualDOM allows batch updates, reduces direct DOM operations, and optimizes the rendering process. VirtualDOM is a strategic tool for Vue.js to optimize UI updates.

Vue.js vs. React: Scalability and MaintainabilityVue.js vs. React: Scalability and MaintainabilityMay 10, 2025 am 12:24 AM

Vue.js and React each have their own advantages in scalability and maintainability. 1) Vue.js is easy to use and is suitable for small projects. The Composition API improves the maintainability of large projects. 2) React is suitable for large and complex projects, with Hooks and virtual DOM improving performance and maintainability, but the learning curve is steeper.

The Future of Vue.js and React: Trends and PredictionsThe Future of Vue.js and React: Trends and PredictionsMay 09, 2025 am 12:12 AM

The future trends and forecasts of Vue.js and React are: 1) Vue.js will be widely used in enterprise-level applications and have made breakthroughs in server-side rendering and static site generation; 2) React will innovate in server components and data acquisition, and further optimize the concurrency model.

Netflix's Frontend: A Deep Dive into Its Technology StackNetflix's Frontend: A Deep Dive into Its Technology StackMay 08, 2025 am 12:11 AM

Netflix's front-end technology stack is mainly based on React and Redux. 1.React is used to build high-performance single-page applications, and improves code reusability and maintenance through component development. 2. Redux is used for state management to ensure that state changes are predictable and traceable. 3. The toolchain includes Webpack, Babel, Jest and Enzyme to ensure code quality and performance. 4. Performance optimization is achieved through code segmentation, lazy loading and server-side rendering to improve user experience.

Vue.js and the Frontend: Building Interactive User InterfacesVue.js and the Frontend: Building Interactive User InterfacesMay 06, 2025 am 12:02 AM

Vue.js is a progressive framework suitable for building highly interactive user interfaces. Its core functions include responsive systems, component development and routing management. 1) The responsive system realizes data monitoring through Object.defineProperty or Proxy, and automatically updates the interface. 2) Component development allows the interface to be split into reusable modules. 3) VueRouter supports single-page applications to improve user experience.

What are the disadvantages of VueJs?What are the disadvantages of VueJs?May 05, 2025 am 12:06 AM

The main disadvantages of Vue.js include: 1. The ecosystem is relatively new, and third-party libraries and tools are not as rich as other frameworks; 2. The learning curve becomes steep in complex functions; 3. Community support and resources are not as extensive as React and Angular; 4. Performance problems may be encountered in large applications; 5. Version upgrades and compatibility challenges are greater.

Netflix: Unveiling Its Frontend FrameworksNetflix: Unveiling Its Frontend FrameworksMay 04, 2025 am 12:16 AM

Netflix uses React as its front-end framework. 1.React's component development and virtual DOM mechanism improve performance and development efficiency. 2. Use Webpack and Babel to optimize code construction and deployment. 3. Use code segmentation, server-side rendering and caching strategies for performance optimization.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor