在这种情况下,你可以使用动态绑定校验规则的方式来定义校验规格。首先,你需要在 data 中定义一个变量来存储校验规则,例如:

data() {
  return {
    rules: {}
  }
}

然后,在 mounted 钩子函数中,根据 publicGoods 数组的长度动态生成校验规则。你可以使用 forEach 方法来遍历 publicGoods 数组,并为每个 item 定义对应的校验规则。例如:

mounted() {
  this.publicGoods.forEach(item => {
    this.$set(this.rules, item.firstLine, [
      { required: true, message: '第一行不能为空', trigger: 'blur' },
      // 其他校验规则...
    ])
  })
}

在上述代码中,使用 this.$set 方法来动态添加校验规则,第一个参数是要绑定规则的对象(这里是 this.rules),第二个参数是要绑定规则的属性名(这里是 item.firstLine),第三个参数是校验规则的数组。

最后,在 el-form-item 中,将 prop 属性绑定到 item.firstLine 属性上,例如:

<el-form-item label='第一行:' :prop='item.firstLine' style='margin-bottom:20px'>
  <el-input v-model='item.firstLine' style='width:200px' maxlength='6' placeholder='最多输入6个字'></el-input>
</el-form-item>

这样,每个 item 的第一行输入框都会绑定对应的校验规则。


原文地址: https://www.cveoy.top/t/topic/fCz5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录