如何在 Store Vuex 中加载 DOM 之前调用 api?

How to call an api before loading the DOM in Store Vuex?

我想在加载 DOM 之前执行一个函数以从 API 中获取数据,我知道在 vue 中它是在 Create 之前但是我如何在 VUEX 中执行它商店?

在 VueX 中,您在“actions”中执行异步代码。因此,您创建一个操作并从 beforeCreate 生命周期挂钩或任何您想放置它的地方调用它。

要调用一个动作,您可以使用 this.$store.dispatch('getMatches') 或者您可以将一个动作映射到您的组件方法块:

methods: {
  ...mapActions([
    'getMatches'
  ])
},
beforeCreate() {
  this.getMatches();
}

通常操作也以小写字母开头,因为它们是方法。