maioyan icon indicating copy to clipboard operation
maioyan copied to clipboard

使用watch监听输入框的变化(由于是异步请求,不要使用competed属性

Open yaogengzhu opened this issue 5 years ago • 0 comments

如何监听输入框的异步请求?

watch: {
    query: function(newVal) {
      // console.log(newVal);
      // 发送axios请求
      this.axios.get("/api/searchList?cityId=10&kw=" + newVal).then(res => {
        // console.log(res)
        // console.log(res.data.data.movies);
        if (newVal && res.status === 200 && res.data.data.movies.list) {
          this.movieList = res.data.data.movies.list;
        }
      })
      if (newVal === ''){
        this.movieList.length = 0
      }
    }
  }

注意的是,这里处理的不够细腻,对于函数防抖还没有处理!~ 当然,watch 监听的异步请求完全是没有问题的。

yaogengzhu avatar May 13 '19 16:05 yaogengzhu