Reputation: 804
I'm newbie to Vue 3. I trying to do my first app with Vue 3 + Vuex + Vue route.
Step-by-step:
How to fix that so if browser back button clicked then no second API call for the same data?
Main component
methods: {
...mapActions('characters', ['getCharacters']),
},
mounted() {
this.getCharacters();
},
Store action which makes the request:
actions: {
async getCharacters({ state, commit }, page = state.currentPage) {
try {
const res = await fetch(`${BASE_URL}character/?page=${page}`);
if (res.ok) {
const { info, results } = await res.json();
commit('setCharacters', { list: results });
}
} catch (e) {
console.log(e);
}
},
};
Upvotes: 0
Views: 860
Reputation: 1009
You can check if characters data exists before invoke getCharacters()
computed: {
...mapState('characters', ['characters']),
},
methods: {
...mapActions('characters', ['getCharacters']),
},
mounted() {
if (!this.characters) {
this.getCharacters();
}
},
Upvotes: 1