What are the problems with the dynamic generation of forms in vue?

there is a requirement to implement a dynamic form, which I implemented in this way

<template>
  <div class="container">
    <div class="form-gtoup">
      <input type="text" name="" id=""><button @click="addItem">add</button>
    </div>
    <div class="form-gtoup" v-for="(item,index) in arr" :key="index">
      <input type="text" name="" id=""><button @click="minusItem(index)">minus</button>
    </div>
  </div>
</template>
export default {
  data() {
    return {
      arr:[]
    };
  },
  methods: {
    addItem () {
      this.arr.push("")
    },
    minusItem (index){
      this.arr.splice(index, 1)
    }

form can be generated dynamically by adding arr dynamically.

but if the completed input, deletes the form that fills out 2, the length of arr becomes 1, the form that fills 2 is still there, and the form that fills out 3 is missing, is there any good way to solve this problem?

May.24,2021

you can't do key, with index directly.
you need to do key with other unique values
such as

.
return {
                arr: [],
                index: 1,
            }
key="index"
addItem () {
      this.arr.push(this.index)
                this.indexPP;
    }, 

       
It is not recommended to use index as key, when

data changes.
it is recommended that the data in arr use the form of object, such as [{id: 'abc'}],
to facilitate later expansion. The newly generated available timestamp is used as id value


index to do key. When you delete and add, Key duplication occurs


you can use form-create to dynamically generate forms. Form-create is a dynamic form generation component that can generate forms with dynamic rendering, data collection, validation and submission functions through JSON. And support the generation of any Vue component. Combined with the built-in 17 commonly used form components and custom components, no matter how complex the form can be easily done. github address

ElementUI version

npm I @ form-create/element-ui

Iview version 2.x | 3.x

npm I @ form-create/iview

Iview version 4.x

Menu