Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access module from Vuex Store

I have the following module:

export const ProfileData = {
    state: {
        ajaxData: null;
    },
    getters: {/*getters here*/},
    mutations: {/*mutations here*/},
    actions: {/*actions here*/}
}

and this module is registered in my global store:

import {ProfileData} from './store/modules/ProfileData.es6'
const store = new Vuex.Store({
    modules: {
       ProfileData: ProfileData
    }
});

I have also used the Vue.use(Vuex) and set the store in new Vue({ store: store}) properly. However, when I try to access the ajaxData belonging to the ProfileData module, in one of my components via this.$store.ProfileData.ajaxData, the console shows an undefined error. The same goes for reading the this.$store.ProfileData or this.$store.ajaxData, while this.$store is defined and I am already able to read it. I also see the ProfileData object added to the _modules property of the store in browser's console.

What is that I am doing wrong to access the modules registered to the Vuex? How can I access those?

like image 846
Arnold Zahrneinder Avatar asked Apr 05 '18 17:04

Arnold Zahrneinder


People also ask

How do you access mutations?

In order to gain access to the Mutations system, you must first complete a secondary quest titled Turn and Face the Strange. After playing through a portion of the Blood and Wine main story, a messenger will deliver a letter to Geralt. After reading the letter, the aforementioned quest will become available.

Is pinia better than Vuex?

Pinia has a Simpler API than Vuex Pinia's API is simpler and more intuitive than Vuex. Getting started with state management is much easier even for a junior developer as a lot of boilerplate code that needed to be written between every state change in Vuex has now been removed in Pinia.

How do I get Vuex getters?

The mapGetters helper simply maps store getters to local computed properties: import { mapGetters } from 'vuex' export default { // ... computed: { // mix the getters into computed with object spread operator ... mapGetters([ 'doneTodosCount', 'anotherGetter', // ... ]) } }

Can I call action from mutation Vuex?

In Vuex, actions are functions that call mutations. Actions exist because mutations must be synchronous, whereas actions can be asynchronous. You can define actions by passing a POJO as the actions property to the Vuex store constructor as shown below. To "call" an action, you should use the Store#dispatch() function.


1 Answers

Directly accessing state of Vuex module

The format to access a Module's local state is $store.state.moduleName.propertyFromState.

So you would use:

this.$store.state.ProfileData.ajaxData

Demo:

const ProfileData = {
  state: {ajaxData: "foo"}
}
const store = new Vuex.Store({
  strict: true,
  modules: {
    ProfileData
  }
});
new Vue({
  store,
  el: '#app',
  mounted: function() {
  	console.log(this.$store.state.ProfileData.ajaxData)
  }
})
<script src="https://unpkg.com/vue/dist/vue.min.js"></script>
<script src="https://unpkg.com/vuex"></script>

<div id="app">
  <p>ajaxData: {{ $store.state.ProfileData.ajaxData }}</p>
</div>

Getters, Actions and Mutators of modules, how to directly access them?

It depends if they are namespaced or not. See demo (explanation in comments):

const ProfileDataWithoutNamespace = {
  state: {ajaxData1: "foo1"},
  getters: {getterFromProfileDataWithoutNamespace: (state) => state.ajaxData1}
}
const ProfileDataWithNamespace = {
  namespaced: true,
  state: {ajaxData2: "foo2"},
  getters: {getterFromProfileDataWithNamespace: (state) => state.ajaxData2}
}
const store = new Vuex.Store({
  strict: true,
  modules: {
    ProfileDataWithoutNamespace,
    ProfileDataWithNamespace
  }
});
new Vue({
  store,
  el: '#app',
  mounted: function() {
    // state is always per module
  	console.log(this.$store.state.ProfileDataWithoutNamespace.ajaxData1)
    console.log(this.$store.state.ProfileDataWithNamespace.ajaxData2)
    // getters, actions and mutations depends if namespace is true or not
    // if namespace is absent or false, they are added with their original name
    console.log(this.$store.getters['getterFromProfileDataWithoutNamespace'])
    // if namespace is true, they are added with Namespace/ prefix
    console.log(this.$store.getters['ProfileDataWithNamespace/getterFromProfileDataWithNamespace'])
  }
})
<script src="https://unpkg.com/vue/dist/vue.min.js"></script>
<script src="https://unpkg.com/vuex"></script>

<div id="app">
  <p>Check the console.</p>
</div>
like image 182
acdcjunior Avatar answered Nov 23 '22 19:11

acdcjunior