[Vuejs]-Vuejs simplify vuex action call

2๐Ÿ‘

โœ…

You should change your syntax:

actions: {
    submitPost(state, data) {
      console.log(data)
    {
๐Ÿ‘คbill.gates

1๐Ÿ‘

I have divided my store into modules so I am exporting my all mutations, actions, getters.
I am committing a mutation from my action with payload,here payload is an object with property currentWidth

export function fetchDeviceCurrentWidth ({ commit, state }, payload) {
  commit('updateDeviceCurrentWidth', payload)
}

My mutation

export function updateDeviceCurrentWidth (state, payload) {
  state.currentDeviceWidth = payload.currentWidth
}

My getter

export const currentDeviceWidth = state => state.currentDeviceWidth

This is from where I am dispatching an action:

myEventHandler () {
      this.$store.dispatch('fetchDeviceCurrentWidth', {
        currentWidth: window.innerWidth
      })
    }

There are many ways to use vuex store, but I like to break store into modules depending on features or Components.

๐Ÿ‘คShivam Bisht

Leave a comment