Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kamaladenalhomsi/4e8aa0e6cf19d3b751f1eec6512a41c5 to your computer and use it in GitHub Desktop.
Save kamaladenalhomsi/4e8aa0e6cf19d3b751f1eec6512a41c5 to your computer and use it in GitHub Desktop.
Traditional way to fetch and store data in vuex
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