Created
July 18, 2018 05:08
-
-
Save sathiyaseelan/c0a46cfdc76e0dff3d57abf81fe4d3d4 to your computer and use it in GitHub Desktop.
Nuxt Example for modularised Vuex Store with multiple nuxtServer Init
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
export const actions = { | |
async nuxtServerInit ({ dispatch }, { app }) { | |
console.log('calling init methods') | |
await dispatch('providers/nuxtServerInit', { app }) | |
await dispatch('products/nuxtServerInit', { app }) | |
} | |
} |
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
export const state = () => ({ | |
products: [] | |
}) | |
export const getters = { | |
getProduct (state, id) { | |
if (state.products) { | |
return state.products.find( p => p.id == id); | |
} | |
return undefined; | |
}, | |
isEmpty (state){ | |
return !state.products || state.products.length == 0 | |
} | |
} | |
export const mutations = { | |
setProducts(state, products) { | |
state.products = products | |
} | |
} | |
export const actions = { | |
async nuxtServerInit ({ commit }, { app }) { | |
console.log('calling products init method') | |
let { data } = await app.$axios.$get('https://api.myjson.com/bins/12u91y') | |
commit('setProducts', data.data) | |
} | |
} | |
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
export const state = () => ({ | |
providers: [] | |
}) | |
export const getters = { | |
getProvider (state, id) { | |
if (state.providers) { | |
return state.providers.find( p => p.id == id); | |
} | |
return undefined; | |
}, | |
isEmpty (state){ | |
return !state.providers || state.providers.length == 0 | |
} | |
} | |
export const mutations = { | |
setProviders(state, providers) { | |
state.providers = providers | |
} | |
} | |
export const actions = { | |
async nuxtServerInit ({ commit }, { app }) { | |
console.log('calling providers init method') | |
let { data } = await app.$axios.$get('https://api.myjson.com/bins/xy7v2') | |
commit('setProviders', data.data) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment