我知道大家都和我一样懒的,嗯~ o( ̄▽ ̄)o,不要脸地这么认为了,嘿嘿,下面直接上代码

<input v-model="keyword" @input="searchEvent"  />
data() {
        return {
            keyword: '',            
            timer: null,
            len: false
        }
    },

实现了汉字输入点击空格后搜索
连续输入以最后一次为准,间隔500ms。

searchEvent() {
            this.clearTimer()
            if (this.keyword && this.keyword.length > 0) {
                this.len = true
                this.timer = setTimeout(() => {
                  	console.log(this.keyword)
                    //此处为接口函数
                }, 500)
            } else {
                if (this.len) {
                    	console.log(this.keyword)
                    //此处为接口函数
                }
                if (this.keyword === '') {
                    this.len = false
                    return
                }
            }
        },
        clearTimer() {
            if (this.timer) {
                clearTimeout(this.timer)
            }
        },
Logo

前往低代码交流专区

更多推荐