Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!