V
V
Vitalionus2019-11-09 19:23:12
Vue.js
Vitalionus, 2019-11-09 19:23:12

Why does the store.getters in the Router get beforeEnter before the axios in the Vuex actions has completed?

In actions there is an axios request
But in the router in beforeEnter store.getters receives old data and does not have time to receive new ones
. What is the reason?

path: '/test',
    name: 'test',
    component: test,
    beforeEnter: (to, from, next) => {
      if (!store.getters.get_test_id) {
        next({ name: 'error' });
      } else {
        next();
      }
    },

It's in store
state: {
test_id: null
},
mutations: {
    set_test_id(state, n) {
      state.test_id = n;
    },
  },
getters: {
    get_test_id: state => state.test_id,
  },
actions: {
    regID({ commit }) {
      axios.post('/site/')
        .then((response) => {
          commit('set_test_id', response.data); // response.data = 555
        });
    },
  },

How to make sure that the router has time to get the test_id changed?
In main.js
const app = new Vue({
  router,
  render: h => h(App),
  store,
  created() {
    store.dispatch('regID');
  },

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question