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