Answer the question
In order to leave comments, you need to log in
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'
})
}
mounted() {
this.getIP()
},
methods: {
async getIP () {
const response = await Service.getIP();
}
}
import Api from '@/services/Api'
export default {
getIP () {
return Api().get('/')
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question