for..in loops iterate over the entire prototype chain, which is virtually never what you want.

for..in loops iterate over the entire prototype chain, which is virtually never what you want. 

意思是使用for..in会遍历整个原型链,这样不是很好的实现方法,推荐使用Object.keys

formRules: {
  name: true,
  cardType: true,
  certificateNo: true
 },
 formData: {
  name: '',
  certificateType: '',
  certificateNo: ''
 }

原代码

for (const key in this.formData) {
     if (!this.formData[key]) { this.formRules[key] = false; valid = false }
     if (this.formData[key]) this.formRules[key] = true
}

修改后的代码

Object.keys(this.formData).forEach(key => {
 if (!this.formData[key]) { this.formRules[key] = false; valid = false }
 if (this.formData[key]) this.formRules[key] = true
})

 

posted @ 2021-04-26 08:54  浅笑19  阅读(415)  评论(0编辑  收藏  举报