vue3 动态生成表单

vue3 动态生成表单

具体需求就是点击添加按钮,生成一个和上面一样的表单,点击删除删除一个表单,当只有一个表单时不能删除

如图:
在这里插入图片描述
当我接到这个需求的时候,脑子第一映像就是转换成数组的增删array.push(),array.pop().就可以了。

那么直接上代码把

<template>
        <el-row>
          <el-button type="primary" @click="addFeedBackForm">添加</el-button>
          <el-button type="danger" @click="delFeedBackForm">删除</el-button>
        </el-row>
    <template v-for="(item, index) in addForms" :key="index">
      <el-form
        class="addForm"
        label-width="100px"
        :model="item"
      >
        <el-form-item label="反映的问题" label-width="100px" prop="item.feedback">
          <el-input v-model="item.feedback" />
        </el-form-item>
        <el-form-item label="问题描述" label-width="100px" prop="item.describe">
          <el-input v-model="item.describe" />
        </el-form-item>
      </el-form>
    </template>
 
</template>

<script>

export default defineComponent({
  name: 'DynamicsCreateForm',
  setup() {
    const state = reactive({
      addForms: [
        {
          feedback: '',
          describe: '',
        }
      ],
    })

    // 添加问题反馈表单
    const addFeedBackForm = () => {
      const addFormItem = {
          feedback: '',
          describe: '',
      }
      state.addForms.push(addFormItem)
    }

    // 删除问题反馈表单
    const delFeedBackForm = () => {
      if (state.addForms.length === 1) {
        $baseMessage('只有一项不能再删了', 'error', 'vab-hey-message-error')
        return
      }
      state.addForms.pop()
    }


    return {
      ...toRefs(state),
      addFeedBackForm,
      delFeedBackForm,
    }
  },
})
</script>
<style lang="scss" scoped>
.addForm {
  border: 1px solid #d0cdcd;
  border-radius: 5px;
  padding: 20px 30px;
  margin-bottom: 30px;
}
}
</style>
### 创建 Vue 3 动态表单 为了实现在 Vue 3动态生成表单的功能,可以采用基于 JSON 配置的方式构建表单组件。这种方法允许开发者通过修改配置文件来调整表单结构而不必更改实际代码。 #### 定义表单配置 首先定义一个用于描述表单项及其属性的对象数组: ```javascript const formConfig = [ { type: &#39;input&#39;, label: &#39;用户名&#39;, model: &#39;username&#39; }, { type: &#39;password&#39;, label: &#39;密码&#39;, model: &#39;password&#39; } ]; ``` 此对象中的每一项都代表了一个独立的输入控件[^2]。 #### 构建动态渲染逻辑 接着,在 Vue 组件内部利用 `v-for` 指令遍历上述配置并根据每条记录创建对应的 HTML 元素: ```html <template> <div class="dynamic-form"> <component v-for="(item, index) in formConfig" :key="index" :is="getComponent(item.type)" v-bind="item"></component> </div> </template> <script> export default { data() { return { formConfig }; }, methods: { getComponent(type) { switch (type) { case "input": return () => import(&#39;./components/InputField.vue&#39;); case "password": return () => import(&#39;./components/PasswordField.vue&#39;); // 可继续扩展其他类型的字段... default: throw new Error(`未知类型 ${type}`); } } } }; </script> ``` 这段脚本展示了如何依据传入的不同 `type` 值加载不同的子组件[^3]。 #### 处理表单数据绑定 为了让这些动态生成表单元素能够正常工作,还需要设置双向数据绑定机制以便收集用户的输入值。这可以通过给每个 `<component>` 添加 `.sync` 或者使用 `v-model` 来完成。 ```html <!-- 使用 v-model 进行简单的双向绑定 --> <input-field v-if="item.type === &#39;input&#39;" v-model="formData[item.model]" /> <!-- 对于更复杂的场景可能需要用到 .sync --> <custom-component v-else-if="item.type !== &#39;input&#39;" :value.sync="formData[item.model]" /> ``` 以上就是关于在 Vue 3 上实现动态表单的一个基本框架[^1]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jieyucx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值