Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question