vue 给el-button添加加载效果

204 阅读1分钟

需求:避免点击开始考试时,用户在同一时刻多次点击(防止重复提交的问题) (1)给按钮绑定( :loading="btnLoading")

 <el-button type="primary" 
 :loading="btnLoading"
  @click="startTest">{{test.paperId ? '继续' : '开始'}}测试</el-button>

(2)在data中初始化 btnLoading:

data () {
      return {
        btnLoading: false
      }
    },


(3)点击开始考试时设置:

async startTest (type) {
        if (this.btnLoading) return
        this.btnLoading = true   //在考卷没有出来时,加载效果
        let _test = this.test
        let _params = {
          configId: _test.id,
          paperId: type === 'result' ? null : _test.paperId
        }
        this.testShow = false
        let { data } = await startTestApi(_params)
        this.btnLoading = false  //在考卷出来后,关闭加载效果
        if (data.success) {
          this.testData = data.operateCallBackObj
          this.testShow = true
        } else {
          this.$message({
            message: data.operateMessage,
            type: 'error'
          })
        }
      }