美文网首页Vue前端开发那些事儿
input输入搜索防抖函数

input输入搜索防抖函数

作者: 飞鹰_007 | 来源:发表于2020-12-21 18:11 被阅读0次

    输入框搜索频繁请求接口,防抖函数可有效控制接口请求完成后,间隔设置的时长再次请求,避免一次请求未结束又进行了下一次请求。

    1.安装lodash: npm install lodash --save

    2.组件中使用:

    import _ from 'lodash'

    <input v-model="mobile"  onkeyup="value=value.replace(/[^\d]/g,'')"  @input="searchInfo" />

    searchInfo: _.debounce(function() {

           this.search();

     }, 200),

    search(){

        if(this.mobile){

            console.log('接口查询')

        }

    }

    注:debounce若使用箭头函数,对this指向无效

    相关文章

      网友评论

        本文标题:input输入搜索防抖函数

        本文链接:https://www.haomeiwen.com/subject/rifdnktx.html