Search code examples
vue.jsvue-componentvuexsyncfusion

vuejs - template, store, this.$store is undefined


I have a problem retreiving the vuex store in a vuejs component. My architecture is pretty straight-forward. I have a store with two modules.

main.js

new Vue({
  el: '#app',
  store,
  router,
  template: '<App/>',
  components: {
    App
  },

Events.vue - Here I use my custom component UserDropdown in a syncfusion component, but I dont think that's relevant. The first registers the UserDropdown, the second fragment will be called when you click on a cell and returns my custom component:

...
components: {
            UserDropdown
        },
...
editTemplate: function () {
   return {template: UserDropdown}
},
...

UserDropdown.vue - here I'd like to use the store, but the result is: "this.$store is undefined". Access to the store from within other components like Events.vue works just fine.

computed: {
        users: function () {
            return this.$store.state.usersState.users;
        }

store.js

import Vue from 'vue';
import Vuex from 'vuex';
import projectsState from './modules/projectsStore'
import usersState from './modules/usersStore'
import createLogger from 'vuex/dist/logger'

Vue.use(Vuex);

const debug = process.env.NODE_ENV !== 'production';

export const store = new Vuex.Store({
  modules: {
    projectsState,
    usersState
  },
  strict: debug,
  plugins: debug ? [createLogger()] : []
});

Why doesnt that work? Is the problem related to the "template: UserDropdown"? I thought every component should be able to access the store...


Solution

  • As it looks, one has to import the store once again in the UserDropdown.vue component. That doesn't make any sense to me, since I imported the store in the new Vue instance as shown above. Here the code fragments to be added in the UserDropdown.vue

    ...
    import {store} from "../store/store";
    ...
    export default {
        store,
        name: 'UserDropdown',
    ...
    ...mapGetters({users: 'usersState/AllUsers'})
    ...