Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

219
Visualizações
Vue router being loaded before Vuex and the app

When the vue app is being created, it will dispatch an action from the store that hits the backend endpoint to fetch the user data and update the user state.

I wanted to use navigation guards (either beforeEnter inside the relevant route or beforeRouteEnter inside the component) so it will check if the user data has the right permissions to route to the component. However, when I try to access either store.state or store.getters, it returns the empty data because the router is loaded before the action in the store is completed yet.

I tried different solutions I found online but not much luck yet..The following is a snippet of the code.

export default {
  name: 'App',
  created() {
    this.setupAxios()
    this.fetchInitial()
  },
  methods: {
    setupAxios(){
      axios.defaults.headers.common['X-CSRFToken'] = window.csrfToken
    },
    fetchInitial(){
      this.$store.dispatch('FETCH_USER_PROFILE')
    },
  }
}
const actions = {
  ['FETCH_USER_PROFILE']: ({commit, dispatch}) => {
    return axios.get(reverseJS['api2:users-list']())
      .then(resp => {
        commit('FETCH_USER_PROFILE', resp.data)
        return resp.data
      })
      .catch(error => {
        commit('USERS_ERROR', error)
        throw error
      })
  },
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda