有些场景,比如表单验证的时候,只要有一个字段没有填写,就给出toast
提示,这就需要一发现问题,就给出提示,并且跳出循环。
map
想要直接跳出循环,需要使用抛出异常的写法,而for of
则适用这种场景。
使用for of
// 进行提交前的验证
valid() {for (const key in this.form) {if (this.form[key] == null || this.form[key] == undefined) {$wuxToast().show({type: 'text',duration: 1000,color: '#fff',text: this.validText[key],success: () => console.log('已完成')})return false}}return true
}