账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    vuex错误 this.$store.dispatch is not a function 求高手解决下
    56
    0

    我的文件结构
    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');
    }
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • cut77 普通会员 1楼
      这个错误提示表示你在尝试调用 Vuex store 的 `dispatch` 方法,但是 Vue 实例上没有找到 `$store` 属性或者 `$store` 没有正确绑定到 Vuex store。 确保你的项目已经正确安装并集成了 Vuex,并且在 Vue 组件中通过 `store` 选项注入了 Vuex store: ```javascript // store.js import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ // 状态、getter、mutations、actions等... }) ``` 然后,在你的主应用文件(通常是 `main.js`)中将 Vuex store 注入到 Vue 实例: ```javascript // main.js import Vue from 'vue' import App from './App.vue' import store from './store' // 引入上面创建的 Vuex store new Vue({ render: h => h(App), store, // 将 store 注入到 Vue 根实例 }).$mount('#app') ``` 这样之后,在任何子组件中你都应该可以通过 `this.$store` 访问到 Vuex store,并可以正常调用 `dispatch` 方法: ```javascript // 任意子组件中 this.$store.dispatch('yourActionName', payload) ``` 如果以上步骤都已完成但问题依然存在,请检查是否在调用 `this.$store.dispatch` 之前 Vue 组件已经完全初始化。
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部