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

165
Visualizações
Vuex + Vue pull state from server just once

Within a History view, I pull down history from backend server by calling the Vuex action in created(), and pass the data to the history table using a computed function, history() that accesses the history module state.

The problem is, each time I return to the History view, a new requests to the server is made due to calling the action in created().

How can I setup so the call is made the first time the view is accessed, and rely on history state for subsequent visits to this view?

History.vue

<script>
import { mapActions } from "vuex";
export default {
  name: "History",
  data() {
    return {
        // data
    };
  },
  methods: {
    ...mapActions(["fetchHistory"]),
    refresh() {
        this.fetchHistory() 
  },
  computed: {
    history() {
      return this.$store.state.history.records;
    },
  },
  created() {
    this.refresh();
  },
};
</script>

historyModule.js

async fetchHistory({commit}){
    await axios.get('api/history')
        .then((response) => {
            commit('setHistory', response.data)
            return Promise.resolve();
        })
        .catch((error) => {
            commit('setHistory', [])
            return Promise.reject(error)
        })
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If history.records is array you can check length before dispatching actions:

created() {
  if(!this.$store.state.history.records.length) this.refresh();
},

If you want this pattern to work across components, you can add the same behavior to the Vuex action instead of to the created hook. This will also keep all of your Vuex behaviors inside of your store code.

about 4 years ago · Juan Pablo Isaza Relatório
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