Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
811 views
in Technique[技术] by (71.8m points)

vue.js - vuex unknown action type: events/loadAll

I am hoping someone can help me out. I moved all my modules from the store/index.js file to a new store/events.js files to clean things up. I am having troubles getting the namespace right for the loadAll action from event.js. Not sure what I am missing here as I got followed some documentation and believe this should be right. I included App.js where I am trying to us "this.$store.dispatch('events/loadAll')" to get the loadAll action. You can see the loadCurrent action is also dispatched in similar fashion and works just fine. Any help would be appreciated.

index.js

import Vue from 'vue'
import Vuex from 'vuex'
import snackbarModule from './snackbar';
import eventsModule from './events';
import usersModule from './users';

Vue.use(Vuex)

export default new Vuex.Store({
  modules: {
    snackbar: snackbarModule,
    eventNames: eventsModule,
    users: usersModule
  }
})

event.js

import Api from "../service/api"

export default {
  namespaced: true,
  state: {
    eventNames: [],
  },
  mutations: {
    SET_EVENTNAMES(state, eventNames) {
        state.eventNames = eventNames
    }
  },
  actions: {
    async loadAll({commit}){
        let response = await Api().get('/event-names')
        let eventNames = response.data.data
        eventNames.forEach(e => {
          e.attributes.id = e.id
        })
        commit('SET_EVENTNAMES', eventNames.map(e => e.attributes))
    }
  }
}

App.vue

<script>
import { mapState } from 'vuex';
  export default {
    name: 'App',
    created(){
        this.$store.dispatch('events/loadAll');
        this.$store.dispatch('users/loadCurrent');
    },
    computed: {
    ...mapState({
      currentUser: state => state.users.currentUser,  
      snackbars: state => state.snackbar.snackbars
    })
    },
    data: () => ({ 
      drawer: null,
      items: [
        { title: 'Schedule', icon: 'mdi-calendar-month', to: '/' },
        { title: 'Results', icon: 'mdi-calendar-check', to: '/Results' },
        { title: 'Points', icon: 'mdi-format-list-numbered', to: '/Points' },
        { title: 'About', icon: 'mdi-help-box', to: '/About' },
      ],
    }),
    methods: {
    logoutUser() {
      this.$store.dispatch("users/logout");
    }
  },
  }
</script>
question from:https://stackoverflow.com/questions/65874970/vuex-unknown-action-type-events-loadall

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. I can see the following row in the index.js:

    import eventsModule from './events';
    

    But you listed the event.js file. Is this a typo? Have you listed the content of the events.js?

  2. If you want to call the action as this.$store.dispatch('events/loadAll') you have to change the module name:

    ...
    
    export default new Vuex.Store({
      modules: {
        snackbar: snackbarModule,
        events: eventsModule, // <- change the key name to 'events'
        users: usersModule
      }
    })
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...