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
})