工作生活

模糊查询,节流减少接口请求次数

2019-07-01  本文已影响0人  考拉_2044

```htmL

<input @blur="blurTWO" v-debounce="searchTWO" v-model="inputAirTWO" />

```

```js

    // 模糊查询请求次数频繁,

    // 解决思路是

    //(1)自定义事件,触发节流

            directives: {

                  debounce: {

                            inserted: function (el, binding) {

                            let timer

                            el.addEventListener('keyup', () => {

                                if (timer) {

                                clearTimeout(timer)

                                }

                                timer = setTimeout(() => {

                                binding.value()

                                }, 300)

                            })

                            }

                        }

                        },

    //(2)绑定自定义事件调用方法 

    methods: {

      search () {

      // 实际要进行的操作 axios.get('')之类的

        this.count++

        console.log('count is:' + this.count)

      }

    }

```

上一篇 下一篇

猜你喜欢

热点阅读