reset state in vuex

  • Hey Community!
    my action :

    export function addItem({  commit,dispatch}, item) {
      return new Promise((resolve, reject) => {
          .post(_api.SAVE_item, item)
          .then((response) => {
          .catch((error) => {
            let errorsMsg = _get(error, '')
            commit('errors', errorsMsg)

    i use this action in two form :save item and update item ,

    and I get errors by my getter in computed property :

     ...mapGetters("item", ["errors"])

    my issue is if errors contain a value after using save form , when I open update form it’s still show me the same value of errors .
    What I need is to reset the value of errors each time I change the form .

    any help !!

    Thanks in advance 😁

  • Create a mutation that call the state object (errors) and reinitialise it to null or " " .

    Then commit the mutation on created hook of save and update form page.

    I will write you the code if you still don’t get it.

  • @syflex do you mean to create another mutation ?? because normaly i have already this one where I commit the value of errors generated after catch :

    export const errors = (state, errors) => {
      state.errors = errors

    and this is my getter:

    export const errors = (state) => {
      return state.errors

    and here is my state:

    errors: {}

  • export const clear_errors = (state, errors) => {
    state.errors = β€œβ€

    is the quickest way to go and just commit it when you need to clear state.errors

  • Thanks that works πŸ‘

Log in to reply