Reset Vuex Store

This entry was posted in Vue.js on 2022-04-23 by Eddy
315 Vue.js

In store folder, create index.js file

const getDefaultState = () => {
  return {
    items: [],
    status: 'empty'
  }
}
// initial state
const state = getDefaultState()
const mutations = {
  RESET_STATE(state) {
    Object.assign(state, getDefaultState())
  }
}
</span>
const actions = {
  resetState ({ commit }) {
    commit('RESET_STATE')
  },
}
export default {
  state,
  getters: {},
  actions,
  mutations
}

You can now reset the store wherever you want, for exemple on logout with:

this.$store.dispatch('resetState')

Comments

No results found.

Write comments

Math, for example, 45-12 = 33

Subscribe Now

You will receive a notification to each new article.

Tag Cloud

  • Composer
  • Yii2
  • OpenGraph
  • Helper
  • Wordpress
  • PhpMyAdmin
  • .htaccess
  • Config
  • Production
  • Git
  • Api
  • Mailchimp
  • Github
  • Vue.js