如何在axios之后调用更新的钩子?

时间:2018-04-04 09:52:24

标签: vue.js axios

在更新的钩子中,我调用一个方法,通过操作存储数据:

挂钩更新:

updated() {
  this.storeOptions()
},

方法:

storeOptions() {
    this.$store.dispatch("storeFilters", {
      idCategory: this.idCategory,
      genres: this.genres,
      priceFrom: this.optionsSlider.value[0],
      priceTo: this.optionsSlider.value[1],
      place: this.activePlace.id,
      count: 6
    });
  }

问题是现在钩子'更新'在组件渲染后立即调用2次(一次又一次获取滑块选项,获取下拉列表选项)。这没有优化......

我尝试了类似的东西

updated() {
  this.$nextTick(function () {
    this.storeOptions()
  })
},

但它没有帮助......

在挂钩中调用axios

mounted() {
  axios
    .get(
      `/api/categories/filterParams.php?categoryId=${
        this.idCategory}`
    )
    .then(response => {
      this.categories = response.data;
      this.optionsSlider.min = +this.categories.minPrice;
      this.optionsSlider.max = +this.categories.maxPrice;
      this.optionsSlider.value = [
        this.optionsSlider.min,
        this.optionsSlider.max
      ];
      this.filterPlacesCategory();
    });
},

1 个答案:

答案 0 :(得分:3)

而不是在更新挂钩上调用this.storeOptions()。您可以在then调用的axios中调用它,并将其挂钩到可以从该调用中受益的任何其他必要事件,例如{1}}或@click你有任何输入。

相关问题