Javascript 如何在Vue中设置变量之前(取消公告)方法函数?

Javascript 如何在Vue中设置变量之前(取消公告)方法函数?,javascript,vue.js,vuejs2,Javascript,Vue.js,Vuejs2,在调用函数之前,如何设置isLoading=true?现在isLoading为false,直到调用debounced函数 <script> import _ from 'lodash' export default { data: { isLoading: false; } methods: { fetch: _.debounce(function() { this.isLoading = true; // fetch my da

在调用函数之前,如何设置isLoading=true?现在isLoading为false,直到调用debounced函数

<script>
import _ from 'lodash'
export default {
  data: {
     isLoading: false;
  }
  methods: {
    fetch: _.debounce(function() {
      this.isLoading = true;
      // fetch my data
      this.isLoading = false;
    }, 200)
  }
}
</script>

我认为,这应该可以做到:

methods: {
  fetch: _.debounce(function() {
    // fetch my data
    this.isLoading = false;
  }, 200),
  askForData: function() {
    this.isLoading = true;
    this.fetch();
  }
}

现在,每当需要新数据时(我猜是为了用户操作),就调用askForData。这会将加载设置为true并调用fetch。由于fetch已取消公告,因此任何其他直接调用fetch都不会触发另一个http请求,但现在已正确处理加载状态

您是对的,但我通过在watch属性中设置isLoading来修复它,这将触发fetch方法