A
A
alaskafx2021-08-29 17:00:08
Vue.js
alaskafx, 2021-08-29 17:00:08

Why does Vuex work “randomly” and how to properly get state from it?

There is storage :

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userData: ''
  },
  mutations: {
    setData: function(state, data) {
      state.userData = data
    }
  },
  actions: {
  },
  modules: {
  }
})


There is a component that gets this very userData from the storage:

export default {
        name: 'Profile',
        data() {
            return {
                userData: this.$store.state.userData
            }
        },
    }


The problem is that this works completely randomly:
It may or may not show userData.
Basically, a component renders either an empty string or undefined.


I don't understand what's wrong - how to solve this problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
PavelPlot99, 2021-08-30
@PavelPlot99

You need to create getters like this:

getters: {
        userData: state => {
            return state.UserData;
        }

And then get the object from the storage like this:
this.$store.getter.userData

S
Sergey Levchenko, 2021-08-30
@nuykon

Asynchrony!!!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question