Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
Can't dispatch in mounted? vuex

Does anyone know why my state.pages doesnt get filled with the json data I fetch with axios? However, when I change something in my file and save it, so that vite will reload, the data does show up on the page. And will dissapear again when I refresh the page in the browser.

PageView:

<template>
  <main v-if="page">
    <h1>{{ page.title }}</h1>
    <div class="content" v-html="page.content"></div>
  </main>
  <main v-else>
    <h1>loading</h1>
  </main>
</template>

<script>
import { mapGetters, mapActions } from "vuex";

export default {
  data() {
    return {
      page: null,
    };
  },
  methods: {
    ...mapActions({ getPages: "getPages" }),
  },
  computed: {
    slug() {
      return this.$route.params.slug;
    },

    getPage() {
      console.log(this.$store.state.pages);
      return (this.page = this.$store.state.pages.find(
        (page) => page.slug === this.slug
      ));
    },
  },
  mounted() {
    this.$store.dispatch("getPages");

    this.getPages();
    this.getPage;
  },
};
</script>

<style>
</style>

Vuex index:

import { createStore } from 'vuex';
import axios from 'axios';

const store = createStore({
  state() {
    return {
      pages: [],
    };
  },
  mutations: {
    setPages(state, { response }) {
      state.pages = response.data;
    },
  },
  actions: {
    getPages({ commit }) {
      axios.get('../src/data/pages.json').then((response) => {
        commit('setPages', { response });
      });
    },
  },
  getters: {},
});

export default store;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The race condition results because promises weren't correctly chained. As a rule of thumb, every function that uses promises should chain all promises in use and return a promise as a result of its work.

getPages is dispatched twice.

It should be:

getPages({ commit }) {
  return axios....

And:

  mounted() {
    return this.getPages()
    .then(() => {
      ...
    });
  },
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda