简体   繁体   English

如何清空以下数组而又不会在Vue手表内部造成无限外观?

[英]How to empty the following array without causing in infinite look inside this Vue watch?

I'm watching a series of fields: 我正在看一系列领域:

fields: {
  handler (fields) {
    Object.entries(fields).forEach(([key, value]) => {
      const field = fields[key]
      // field.errors = [] this will trigger an infinite look
      if (!field.validation) return
      const isRequired = field.validation.isRequired && !field.value
      if (isRequired) {
        field.errors[field.errors.length] = {
          errorType: 'isRequired',
          message: 'This field is required.'
        }
      }
    })
  },
  deep: true
}

Now I want to reset field.errors before the code runs ... the problem is that changing field, will change fields, and therefore, causing an infinite loop. 现在,我想在代码运行之前重置field.errors ...问题是更改字段会更改字段,因此会导致无限循环。

you can check if the errors array is already empty before setting them to a new empty array 您可以在将错误数组设置为新的空数组之前检查错误数组是否已为空

if (Array.isArray(field.errors) && field.errors.length) 
    field.errors = []

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM