关于vue.js:vue中使用axios取消请求

3次阅读

共计 669 个字符,预计需要花费 2 分钟才能阅读完成。

new Vue({
  el: "#app",
  data: {
      searchItems: null,
      searchText: "some value",
      cancelSource: null,
      infoText: null
  },
  methods: {search() {if (this.searchText.length < 3)
      {return;}

      this.searchItems = null;
      this.infoText = 'please wait 5 seconds to load data';

      this.cancelSearch();
      this.cancelSource = axios.CancelToken.source();

      axios.get('https://httpbin.org/delay/5?search=' + this.searchText, {cancelToken: this.cancelSource.token}).then((response) => {if (response) {
            this.searchItems = response.data;
            this.infoText = null;
            this.cancelSource = null;
          }
        });
    },
    cancelSearch () {if (this.cancelSource) {this.cancelSource.cancel('Start new search, stop active search');
        console.log('cancel request done');
      }
    }
  }
})

原文链接:https://stackoverflow.com/a/5…

正文完
 0