Created
January 13, 2021 12:33
-
-
Save kamaladenalhomsi/4e8aa0e6cf19d3b751f1eec6512a41c5 to your computer and use it in GitHub Desktop.
Traditional way to fetch and store data in vuex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const state => () => ({ | |
cats: { | |
loaded: false, | |
data: [] | |
} | |
}) | |
const mutations = { | |
SET_CATS (state, payload) { | |
// merge payload with state | |
Object.keys(payload).forEach(key => { | |
state[key] = payload[key] | |
}) | |
} | |
} | |
const actions = { | |
async getCats({ commit }) { | |
commit('SET_CATS', { | |
loaded: false | |
}) | |
const cats = await myAPIRequestToFetchCats() | |
commit('SET_CATS', { | |
loaded: true, | |
data: cats | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment