S
S
Spoon in the brain2019-11-02 15:26:29
Vue.js
Spoon in the brain, 2019-11-02 15:26:29

How to pass variable from actions to state (Vuex)?

Good afternoon, I ran into a problem, I seem to be doing everything right, but the variable is not transferred. Here is the code:

import JWTDecode from 'jwt-decode'

export default{
  state:{
    status: '',
    userinfo: '' //мне нужно в это состояние передать переменную userData
  },


  getters:{
    userStatus: (state) => state.status,
    userInfo: (state) => state.userinfo 
  },


  mutations:{
    "user_request": (state) => {
      state.status = 'loading'
    },
    "user_success": (state, userData) => {
      state.status = 'success'
      state.userinfo = userData //тут собственно я и пытаюсь это сделать
    },
    "user_error":(state) => {
      state.status = 'error'
    }
  },


  actions:{
    'user_request': ({commit, dispatch, state}, token) => {
      return new Promise((resolve, reject) => {
        commit('user_request')
        if(localStorage.getItem('user-token')){
          const userData = JWTDecode(token) //а вот и эта переменная
          commit('user_success')
          resolve()
        }else{
          commit('user_error')
          reject()
        }
      })
    }
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
idkw13, 2019-11-02
@vessels

commit('user_success', userData )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question