Vuex Notes

Example Vuex with some comment notes

In main.js we add the store via:

In a component that uses something in the store:

Mutations are for non-async updates to the Vuex store. Actions can use async code. Generally, should actions should always be used instead of Mutators.

If using Mutators.. the update to the store can happen directly. i.e. this.$store.commit('mutatorMethodThatUpdatesStore');

Actions use dispatch instead: this.$store.dispatch('mutatorMethodThatUpdatesStore');

3 useful methods from Vuex are:

mapGetters

mapMutations

mapActions

mapState – mapState should be used instead of creating “dummy” getters. Dummy getters are ones that simply just return the store’s state value.

Getters should be more used for computed type values or for things such as a filter etc.

For each of the 4 above..All have either an array of strings of names of state/getter/mutation/action or can pass in an object with key/value pairs if needing a different name. For example: