then()转换成async await

166 阅读1分钟

转换前:

function submit(){
    this.$refs.form1.validate().then(res => {
            uni.$u.toast('校验通过')
    }).catch(errors => {
            uni.$u.toast('校验失败')
    })
}

转换后:

async function submit() {
     try {
        let res = await this.$refs.form1.validate();
        if(res){//res是true 布尔值
            uni.$u.toast('校验失败')
        }
      } catch (error) {//error是一个数组
        uni.$u.toast('校验失败')
        console.log('error',error) //getdata error
      }
}