this$store 函数未定义

this$store function is undefined

我正在尝试使用 vuex 的商店进行一些 API 调用,但是在安装 vuex 之后,将商店导入我的文件并遵循其他堆栈溢出答案,例如确保安装了 vuex,如果我正在导出我的使用“Vuex.Store”等存储文件,但我的 loadCalls 函数仍然无法正常工作。

这是我得到的错误:

this.$store.loadCalls is not a function

这是我的函数以及我如何尝试调用它,它在我的 store.js 文件的 ACTIONS 部分中声明。

loadCalls() {
   axios
        .get("/some/calls")
        .then(res => {
          console.log(res)
        });
},

当我的组件加载时,我尝试在 beforeMount() 中使用它:

beforeMount(){
    this.$store.loadCalls();
}

我做错了什么?

如果您定义了这样的操作:

actions: {
  loadCalls() {
    // ...
  }
}

那么你可以这样称呼它:

this.$store.dispatch('loadCalls');

操作不会直接公开,您可以使用 dispatch.

调用它们

https://vuex.vuejs.org/guide/actions.html#dispatching-actions