K
K
kr_ilya2019-04-30 21:18:37
Vue.js
kr_ilya, 2019-04-30 21:18:37

How to prevent re-execution of axios request?

There is a SPA application vue. The component makes a request to api using axios. When the page is loaded, a request to the api is made for the first time, then if you go to another page and then go back, the request to the server will be executed again. And so every time you open this page. How to avoid repeated requests to the server and save data from the client until the site is reloaded?
I know a little about vue, just starting to learn. There are several options that have nothing to do with vue mechanics (for example, store data in cookies and just block the call to the api with a condition, but this is still a crutch ...).
api.js file

import axios from 'axios'
export default() => {
  return axios.create({
    baseURL: 'http://' + document.location.host + ':3000'
  })
}

Function in vue component
mounted() {
     this.getIP()
   },
  methods: {
    async getIP () {
      const response = await Service.getIP();
    }
  }

File containing Service:
import Api from '@/services/Api'

export default {
  getIP () {
    return Api().get('/')
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
coderisimo, 2019-04-30
@kr_ilya

You can store data in local storage. Before the request, see if it contains the necessary data. And update according to the condition.
ps this is provided that you have a full page update. If not, then of course Vuex

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question