ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] >[success] # async和await的使用 ~~~ 工作中经常会用到'async'和'await'或者'try'、'catch'、'finally',写法如下,deviceDisable方法中的catch是 confirm的取消按钮点击的时候会进入catch状态,还有的是如果deviceDisable方法需要判断changeDeviceState方法中的 接口是否成功执行,需要在changeDeviceState方法中return成功后的返回值, methods:{ // 禁用方法 async deviceDisable(row){ try{ await this.$confirm('提示', '确定要执行操作吗', { closeOnClickModal: false, confirmButtonText: '确定', cancelButtonText: '取消' }) await this.changeDeviceState(row) }catch(e){ return } }, async changeDeviceState(row){ try{ let deviceState = row.isEnabled deviceState = !deviceState const params = { deviceId: row.id, deviceState: deviceState } await changeDeviceState(params) // 如果使用它的方法中需要用到res返回值就这样写 // const res = await changeDeviceState(params) row.isEnabled = deviceState }catch (e) { this.$message.error(e && e.message) console.warn(e) } } } ~~~