vuex错误 this.$store.dispatch is not a function 求高手解决下

新手上路,请多包涵

我的文件结构
main.js
store/index.js
components/index.vue

main.js

import Vue from 'vue'
import Vuex from 'vuex'
import store from './store'
Vue.use(Vuex)

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

store/index.js

export default {
  state: {
    datalist: []
  },
  mutations: {

  },
  actions: {
    getData() {
      this.$http.get("apiurl")
        .then(response => {
          console.log(response)
          console.log(response.data)
        })
        .catch(err => {
          console.log(err);
        });
    }
  }
}

index.vue

mounted(){
    this.$store.dispatch('getData');
}
阅读 6.8k
4 个回答

Vue.use(Vuex)

请仔细阅读 Vuex 的文档,你并没有创建 Vuex 实例,自然也没有办法调用它的方法。

store/index.js中

export default new Vuex.Store({...})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题