SoFunction
Updated on 2025-04-04

Implementation method of dynamically adding forms for Vue

Dynamic forms are a common requirement in applications, especially when the number and type of form fields need to change dynamically based on user input or system status. The dynamic data binding feature provided by Vue makes it very easy to create such forms. This article will introduce in detail how to create dynamic forms in Vue, and demonstrate specific implementation methods through multiple examples.

Basic concepts and functions

Dynamic Form

Dynamic forms refer to those forms whose fields number or type can be dynamically changed according to business logic. This feature is very useful in many scenarios. For example, when users fill out an order, they will cause different input fields to appear in the form.

Two-way binding

Bidirectional data binding in Vue means that the user's input in the form can be reflected in the data model in real time, and vice versa. Vue can easily implement this binding through the v-model directive.

Example 1: Basic dynamic form items

First, let's look at a simple example showing how to dynamically add and delete form items.

<template>
  <div>
    <div v-for="(field, index) in fields" :key="index">
      <label for="input{{ index }}">{{  }}</label>
      <input v-model="" type="text" >
      <button @click="removeField(index)">Remove Field</button>
    </div>
    <button @click="addField">Add Field</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fields: [
        { label: 'Name', value: '' },
        { label: 'Email', value: '' }
      ]
    };
  },
  methods: {
    addField() {
      ({ label: 'New Field', value: '' });
    },
    removeField(index) {
      (index, 1);
    }
  }
};
</script>

In this example, we usev-forGo throughfieldsArray and create an input box for each field. passv-modelThe instruction implements two-way data binding.

Example 2: Dynamic form and array index

Sometimes, we may need to process the data based on the index of the form field. In this case, it is important to ensure consistency of the index every time a field is added or deleted.

<template>
  <div>
    <div v-for="(field, index) in fields" :key="index">
      <label for="input{{ index }}">{{  }}</label>
      <input v-model="" type="text" >
      <button @click="removeField(index)">Remove Field</button>
    </div>
    <button @click="addField">Add Field</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fields: [
        { label: 'Name', value: '' },
        { label: 'Email', value: '' }
      ]
    };
  },
  methods: {
    addField() {
      ({ label: 'New Field', value: '' });
    },
    removeField(index) {
      (index, 1);
    }
  }
};
</script>

The key here is to useindexTo identify the location of each field and correctly handle the index changes when adding or deleting fields.

Example 3: Use computed properties to process dynamic forms

If we need to perform some complex calculations based on form data, using computed properties can make the code clearer.

<template>
  <div>
    <div v-for="(field, index) in fields" :key="index">
      <label for="input{{ index }}">{{  }}</label>
      <input v-model="" type="text" >
    </div>
    <p>Total Fields: {{ totalFields }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fields: [
        { label: 'Name', value: '' },
        { label: 'Email', value: '' }
      ]
    };
  },
  computed: {
    totalFields() {
      return ;
    }
  },
  methods: {
    addField() {
      ({ label: 'New Field', value: '' });
    },
    removeField(index) {
      (index, 1);
    }
  }
};
</script>

In this example, we use a computed propertytotalFieldsTo count the total number of fields in the current form.

Example 4: Dynamic forms and conditional rendering

Sometimes, we may need to decide whether to display a certain form field based on certain conditions. Vue's conditional rendering feature can help us achieve this function.

<template>
  <div>
    <div v-if="showNameField">
      <label for="name">Name</label>
      <input v-model="" type="text" >
    </div>
    <div v-if="showEmailField">
      <label for="email">Email</label>
      <input v-model="" type="text" >
    </div>
    <button @click="toggleNameField">Toggle Name Field</button>
    <button @click="toggleEmailField">Toggle Email Field</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showNameField: true,
      showEmailField: false,
      formData: {
        name: '',
        email: ''
      }
    };
  },
  methods: {
    toggleNameField() {
       = !;
    },
    toggleEmailField() {
       = !;
    }
  }
};
</script>

In this example, we usev-ifTo conditionally render the form field and toggle the display status of the field through methods.

Usage Tips and Analysis

  • State Management: When dealing with complex forms, the rational use of Vuex or other state management libraries can help better manage form data.
  • Performance optimization: Avoid overuse of compute properties or listeners in large forms to avoid performance impacts.
  • Maintainability: Keeping the code clear and modular helps maintain and expand projects in the future.

Through the above example, we can see how Vue's dynamic form functionality is combined with two-way data binding. Whether you are a beginner in Vue or a developer with some experience, mastering these tips can help you build and manage dynamic forms more effectively.

This is the end of this article about the implementation method of Vue dynamically adding forms. For more related content on Vue dynamically adding forms, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!