问题:一般情况下按钮的点击事件往往无外乎发送ajax请求,接口调用,处理事件等等。当点击一个按钮时候,如果点击的比较快,可能会触发多次的请求,导致接口的无效调用,这时就需要对 按钮点击 进行控制。
解决:
//html层 <el-button :loading="saveLoading" type="primary" @click="handleClick">确定</el-button> //data层 isLoading:false, //按钮点击loading控制,默认为false //事件逻辑 methods: { handleClick() { //事件逻辑处理 this.ajaxRequest() //发送ajax请求 }, async ajaxRequest() { if(this.saveLoading) return //当接口请求没响应之前,loading为true,此时通过该语句return掉不执行下面的操作,从而控制请求次数 this.saveLoading = true const [err, res] = await interface() //接口调用 this.saveLoading = false } }