報錯: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
})

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章