Nuxt + Vuex - How do I break down a Vuex module into separate files?
In the Nuxt documentation (here) it says 'You can optionally break down a module file into separate files: state.js
, actions.js
, mutations.js
and getters.js
.'
I can't seem to find any examples of how this is done - lots of breaking down the Vuex store at the root level into state.js
, actions.js
, mutations.js
and getters.js
, and into individual module files, but nothing about breaking the modules themselves down.
So currently I have:
├── assets
├── components
└── store
├── moduleOne.js
├── moduleTwo.js
└── etc...
And what I would like to have is:
├── assets
├── components
└── store
└── moduleOne
└── state.js
└── getters.js
└── mutations.js
└── actions.js
└── moduleTwo
└── etc...
To try this out, in /store/moduleOne/state.js
I have:
export const state = () => {
return {
test: 'test'
}
};
and in /store/moduleOne/getters.js
I have:
export const getters = {
getTest (state) {
return state.test;
}
}
In my component I'm accessing this with $store.getters['moduleOne/getters/getTest']
However using the debugger and Vue devtools, it seems like state isn't accessible in the getters file - it seems to be looking for a state in the local file, so state.test
is undefined.
Attempting to import state
from my state.js
file into my getters.js
file doesn't seem to work either.
Does anyone have an example of how they've managed to break the store down like this in Nuxt?
javascript vue.js vuex nuxt.js nuxt
add a comment |
In the Nuxt documentation (here) it says 'You can optionally break down a module file into separate files: state.js
, actions.js
, mutations.js
and getters.js
.'
I can't seem to find any examples of how this is done - lots of breaking down the Vuex store at the root level into state.js
, actions.js
, mutations.js
and getters.js
, and into individual module files, but nothing about breaking the modules themselves down.
So currently I have:
├── assets
├── components
└── store
├── moduleOne.js
├── moduleTwo.js
└── etc...
And what I would like to have is:
├── assets
├── components
└── store
└── moduleOne
└── state.js
└── getters.js
└── mutations.js
└── actions.js
└── moduleTwo
└── etc...
To try this out, in /store/moduleOne/state.js
I have:
export const state = () => {
return {
test: 'test'
}
};
and in /store/moduleOne/getters.js
I have:
export const getters = {
getTest (state) {
return state.test;
}
}
In my component I'm accessing this with $store.getters['moduleOne/getters/getTest']
However using the debugger and Vue devtools, it seems like state isn't accessible in the getters file - it seems to be looking for a state in the local file, so state.test
is undefined.
Attempting to import state
from my state.js
file into my getters.js
file doesn't seem to work either.
Does anyone have an example of how they've managed to break the store down like this in Nuxt?
javascript vue.js vuex nuxt.js nuxt
Good question. Just tried it out and it indeed breaks down my app. Maybe create an issue on cmty.app/nuxt. Seems like either a bug or lack of documentation.
– Imre_G
Nov 23 '18 at 13:01
Example is at github.com/nuxt/nuxt.js/tree/dev/test/fixtures/basic/store/foo/… ;)
– manniL
Nov 23 '18 at 15:30
above answer from @manniL doesn't solve the issue, since the contents of the module directory are simply repeated in the file of the same name in the root of the store
– Guy Harper
Nov 29 '18 at 14:27
Sorry, I got a bit more into detail now ;)
– manniL
Nov 29 '18 at 16:33
add a comment |
In the Nuxt documentation (here) it says 'You can optionally break down a module file into separate files: state.js
, actions.js
, mutations.js
and getters.js
.'
I can't seem to find any examples of how this is done - lots of breaking down the Vuex store at the root level into state.js
, actions.js
, mutations.js
and getters.js
, and into individual module files, but nothing about breaking the modules themselves down.
So currently I have:
├── assets
├── components
└── store
├── moduleOne.js
├── moduleTwo.js
└── etc...
And what I would like to have is:
├── assets
├── components
└── store
└── moduleOne
└── state.js
└── getters.js
└── mutations.js
└── actions.js
└── moduleTwo
└── etc...
To try this out, in /store/moduleOne/state.js
I have:
export const state = () => {
return {
test: 'test'
}
};
and in /store/moduleOne/getters.js
I have:
export const getters = {
getTest (state) {
return state.test;
}
}
In my component I'm accessing this with $store.getters['moduleOne/getters/getTest']
However using the debugger and Vue devtools, it seems like state isn't accessible in the getters file - it seems to be looking for a state in the local file, so state.test
is undefined.
Attempting to import state
from my state.js
file into my getters.js
file doesn't seem to work either.
Does anyone have an example of how they've managed to break the store down like this in Nuxt?
javascript vue.js vuex nuxt.js nuxt
In the Nuxt documentation (here) it says 'You can optionally break down a module file into separate files: state.js
, actions.js
, mutations.js
and getters.js
.'
I can't seem to find any examples of how this is done - lots of breaking down the Vuex store at the root level into state.js
, actions.js
, mutations.js
and getters.js
, and into individual module files, but nothing about breaking the modules themselves down.
So currently I have:
├── assets
├── components
└── store
├── moduleOne.js
├── moduleTwo.js
└── etc...
And what I would like to have is:
├── assets
├── components
└── store
└── moduleOne
└── state.js
└── getters.js
└── mutations.js
└── actions.js
└── moduleTwo
└── etc...
To try this out, in /store/moduleOne/state.js
I have:
export const state = () => {
return {
test: 'test'
}
};
and in /store/moduleOne/getters.js
I have:
export const getters = {
getTest (state) {
return state.test;
}
}
In my component I'm accessing this with $store.getters['moduleOne/getters/getTest']
However using the debugger and Vue devtools, it seems like state isn't accessible in the getters file - it seems to be looking for a state in the local file, so state.test
is undefined.
Attempting to import state
from my state.js
file into my getters.js
file doesn't seem to work either.
Does anyone have an example of how they've managed to break the store down like this in Nuxt?
javascript vue.js vuex nuxt.js nuxt
javascript vue.js vuex nuxt.js nuxt
asked Nov 23 '18 at 12:32
Guy HarperGuy Harper
312
312
Good question. Just tried it out and it indeed breaks down my app. Maybe create an issue on cmty.app/nuxt. Seems like either a bug or lack of documentation.
– Imre_G
Nov 23 '18 at 13:01
Example is at github.com/nuxt/nuxt.js/tree/dev/test/fixtures/basic/store/foo/… ;)
– manniL
Nov 23 '18 at 15:30
above answer from @manniL doesn't solve the issue, since the contents of the module directory are simply repeated in the file of the same name in the root of the store
– Guy Harper
Nov 29 '18 at 14:27
Sorry, I got a bit more into detail now ;)
– manniL
Nov 29 '18 at 16:33
add a comment |
Good question. Just tried it out and it indeed breaks down my app. Maybe create an issue on cmty.app/nuxt. Seems like either a bug or lack of documentation.
– Imre_G
Nov 23 '18 at 13:01
Example is at github.com/nuxt/nuxt.js/tree/dev/test/fixtures/basic/store/foo/… ;)
– manniL
Nov 23 '18 at 15:30
above answer from @manniL doesn't solve the issue, since the contents of the module directory are simply repeated in the file of the same name in the root of the store
– Guy Harper
Nov 29 '18 at 14:27
Sorry, I got a bit more into detail now ;)
– manniL
Nov 29 '18 at 16:33
Good question. Just tried it out and it indeed breaks down my app. Maybe create an issue on cmty.app/nuxt. Seems like either a bug or lack of documentation.
– Imre_G
Nov 23 '18 at 13:01
Good question. Just tried it out and it indeed breaks down my app. Maybe create an issue on cmty.app/nuxt. Seems like either a bug or lack of documentation.
– Imre_G
Nov 23 '18 at 13:01
Example is at github.com/nuxt/nuxt.js/tree/dev/test/fixtures/basic/store/foo/… ;)
– manniL
Nov 23 '18 at 15:30
Example is at github.com/nuxt/nuxt.js/tree/dev/test/fixtures/basic/store/foo/… ;)
– manniL
Nov 23 '18 at 15:30
above answer from @manniL doesn't solve the issue, since the contents of the module directory are simply repeated in the file of the same name in the root of the store
– Guy Harper
Nov 29 '18 at 14:27
above answer from @manniL doesn't solve the issue, since the contents of the module directory are simply repeated in the file of the same name in the root of the store
– Guy Harper
Nov 29 '18 at 14:27
Sorry, I got a bit more into detail now ;)
– manniL
Nov 29 '18 at 16:33
Sorry, I got a bit more into detail now ;)
– manniL
Nov 29 '18 at 16:33
add a comment |
2 Answers
2
active
oldest
votes
I am using nuxt 2.1.0
If you want to have something like this :
In my store/index.js
Make sure you have namespaced: true
import Vuex from 'vuex';
import apiModule from './modules/api-logic';
import appModule from './modules/app-logic';
const createStore = () => {
return new Vuex.Store({
namespaced: true,
modules: {
appLogic: appModule,
api: apiModule
}
});
};
export default createStore
In moduleOne
In my store/api-logic/index.js
import actions from './actions';
import getters from './getters';
import mutations from './mutations';
const defaultState = {
hello: 'salut I am module api'
}
const inBrowser = typeof window !== 'undefined';
// if in browser, use pre-fetched state injected by SSR
const state = (inBrowser && window.__INITIAL_STATE__) ? window.__INITIAL_STATE__.page : defaultState;
export default {
state,
actions,
mutations,
getters
}
In my store/api-logic/getters.js
export default {
getHelloThere: state => state.hello
}
In module Two
In my store/app-logic/index.js
import actions from './actions';
import getters from './getters';
import mutations from './mutations';
const defaultState = {
appLogicData: 'bonjours I am module Logic'
}
const inBrowser = typeof window !== 'undefined';
// if in browser, use pre-fetched state injected by SSR
const state = (inBrowser && window.__INITIAL_STATE__) ? window.__INITIAL_STATE__.page : defaultState;
export default {
state,
actions,
mutations,
getters
}
In my store/app-logic/getters.js
export default {
getAppLogicData: state => state.appLogicData
}
Anywhere in the app
computed: {
...mapGetters({
logicData: 'getAppLogicData',
coucou: 'getHelloThere'
})
},
mounted () {
console.log('coucou', this.coucou) --> salut I am module api
console.log('logicData', this.logicData) --> bonjours I am module Logic
}
Bonus Point
If you want to communicate between the modules for example a action in app-logic which trigger something in api-logic.
So app-logic (module one) to api-logic (module two)
When you specify root: true
it will start to look at the root of the store.
In store/app-logic/actions.js
callPokemonFromAppLogic: ({ dispatch }, id) => {
dispatch('callThePokemonFromApiLogic', id, {root:true});
},
In store/api-logic/actions.js
callThePokemonFromApiLogic: ({ commit }, id) => {
console.log('I make the call here')
axios.get('http://pokeapi.salestock.net/api/v2/pokemon/' + id).then(response => commit('update_pokemon', response.data))
},
In store/api-logic/index.js
add another entry
import actions from './actions';
import getters from './getters';
import mutations from './mutations';
const defaultState = {
appLogicData: 'bonjours I am module Logic',
pokemon: {}
}
const inBrowser = typeof window !== 'undefined';
// if in browser, use pre-fetched state injected by SSR
const state = (inBrowser && window.__INITIAL_STATE__) ? window.__INITIAL_STATE__.page : defaultState;
export default {
state,
actions,
mutations,
getters
}
In store/api-logic/mutations.js
add the pokemon mutation :p
update_pokemon: (state, pokemon) => {
state.pokemon = pokemon
}
Anywhere in the app :
computed: {
...mapGetters({
bidule: 'bidule',
pokemon: 'getPokemon'
})
},
mounted() {
console.log('bidule', this.bidule)
this.callPokemonFromAppLogic('1') --> the call
console.log('the pokemon', this.pokemon.name) --> 'bulbasaur'
},
methods: {
...mapActions({
callPokemonFromAppLogic: 'callPokemonFromAppLogic'
}),
}
At the end your Vue devTool should look like this :)
And Voilà I hope It was clear.
Code example :
https://github.com/CMarzin/nuxt-vuex-modules
add a comment |
Your issue
Use default
exports in your files to achieve the desired effect (no named exports except in the index.js
)
Example
An example can be found directly in the Nuxt.js test suite (at https://github.com/nuxt/nuxt.js/tree/dev/test/fixtures/basic/store/foo).
If you'd run the basic
fixture and access the /store page you'll see the following result
The "repeated" contents in the module itself just show that the split-up values take priority (otherwise getVal
wouldn't return 10 but 99 and state.val
wouldn't be 4 but 2).
store.vue code:
<template>
<div>
<h1>{{ baz }}</h1>
<br>
<p>{{ $store.state.counter }}</p>
<br>
<h2>{{ getVal }}</h2>
<br>
<h3>{{ getBabVal }}</h3>
</div>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
computed: {
...mapGetters('foo/bar', ['baz']),
...mapGetters('foo/blarg', ['getVal']),
...mapGetters('bab', ['getBabVal'])
}
}
</script>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446792%2fnuxt-vuex-how-do-i-break-down-a-vuex-module-into-separate-files%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I am using nuxt 2.1.0
If you want to have something like this :
In my store/index.js
Make sure you have namespaced: true
import Vuex from 'vuex';
import apiModule from './modules/api-logic';
import appModule from './modules/app-logic';
const createStore = () => {
return new Vuex.Store({
namespaced: true,
modules: {
appLogic: appModule,
api: apiModule
}
});
};
export default createStore
In moduleOne
In my store/api-logic/index.js
import actions from './actions';
import getters from './getters';
import mutations from './mutations';
const defaultState = {
hello: 'salut I am module api'
}
const inBrowser = typeof window !== 'undefined';
// if in browser, use pre-fetched state injected by SSR
const state = (inBrowser && window.__INITIAL_STATE__) ? window.__INITIAL_STATE__.page : defaultState;
export default {
state,
actions,
mutations,
getters
}
In my store/api-logic/getters.js
export default {
getHelloThere: state => state.hello
}
In module Two
In my store/app-logic/index.js
import actions from './actions';
import getters from './getters';
import mutations from './mutations';
const defaultState = {
appLogicData: 'bonjours I am module Logic'
}
const inBrowser = typeof window !== 'undefined';
// if in browser, use pre-fetched state injected by SSR
const state = (inBrowser && window.__INITIAL_STATE__) ? window.__INITIAL_STATE__.page : defaultState;
export default {
state,
actions,
mutations,
getters
}
In my store/app-logic/getters.js
export default {
getAppLogicData: state => state.appLogicData
}
Anywhere in the app
computed: {
...mapGetters({
logicData: 'getAppLogicData',
coucou: 'getHelloThere'
})
},
mounted () {
console.log('coucou', this.coucou) --> salut I am module api
console.log('logicData', this.logicData) --> bonjours I am module Logic
}
Bonus Point
If you want to communicate between the modules for example a action in app-logic which trigger something in api-logic.
So app-logic (module one) to api-logic (module two)
When you specify root: true
it will start to look at the root of the store.
In store/app-logic/actions.js
callPokemonFromAppLogic: ({ dispatch }, id) => {
dispatch('callThePokemonFromApiLogic', id, {root:true});
},
In store/api-logic/actions.js
callThePokemonFromApiLogic: ({ commit }, id) => {
console.log('I make the call here')
axios.get('http://pokeapi.salestock.net/api/v2/pokemon/' + id).then(response => commit('update_pokemon', response.data))
},
In store/api-logic/index.js
add another entry
import actions from './actions';
import getters from './getters';
import mutations from './mutations';
const defaultState = {
appLogicData: 'bonjours I am module Logic',
pokemon: {}
}
const inBrowser = typeof window !== 'undefined';
// if in browser, use pre-fetched state injected by SSR
const state = (inBrowser && window.__INITIAL_STATE__) ? window.__INITIAL_STATE__.page : defaultState;
export default {
state,
actions,
mutations,
getters
}
In store/api-logic/mutations.js
add the pokemon mutation :p
update_pokemon: (state, pokemon) => {
state.pokemon = pokemon
}
Anywhere in the app :
computed: {
...mapGetters({
bidule: 'bidule',
pokemon: 'getPokemon'
})
},
mounted() {
console.log('bidule', this.bidule)
this.callPokemonFromAppLogic('1') --> the call
console.log('the pokemon', this.pokemon.name) --> 'bulbasaur'
},
methods: {
...mapActions({
callPokemonFromAppLogic: 'callPokemonFromAppLogic'
}),
}
At the end your Vue devTool should look like this :)
And Voilà I hope It was clear.
Code example :
https://github.com/CMarzin/nuxt-vuex-modules
add a comment |
I am using nuxt 2.1.0
If you want to have something like this :
In my store/index.js
Make sure you have namespaced: true
import Vuex from 'vuex';
import apiModule from './modules/api-logic';
import appModule from './modules/app-logic';
const createStore = () => {
return new Vuex.Store({
namespaced: true,
modules: {
appLogic: appModule,
api: apiModule
}
});
};
export default createStore
In moduleOne
In my store/api-logic/index.js
import actions from './actions';
import getters from './getters';
import mutations from './mutations';
const defaultState = {
hello: 'salut I am module api'
}
const inBrowser = typeof window !== 'undefined';
// if in browser, use pre-fetched state injected by SSR
const state = (inBrowser && window.__INITIAL_STATE__) ? window.__INITIAL_STATE__.page : defaultState;
export default {
state,
actions,
mutations,
getters
}
In my store/api-logic/getters.js
export default {
getHelloThere: state => state.hello
}
In module Two
In my store/app-logic/index.js
import actions from './actions';
import getters from './getters';
import mutations from './mutations';
const defaultState = {
appLogicData: 'bonjours I am module Logic'
}
const inBrowser = typeof window !== 'undefined';
// if in browser, use pre-fetched state injected by SSR
const state = (inBrowser && window.__INITIAL_STATE__) ? window.__INITIAL_STATE__.page : defaultState;
export default {
state,
actions,
mutations,
getters
}
In my store/app-logic/getters.js
export default {
getAppLogicData: state => state.appLogicData
}
Anywhere in the app
computed: {
...mapGetters({
logicData: 'getAppLogicData',
coucou: 'getHelloThere'
})
},
mounted () {
console.log('coucou', this.coucou) --> salut I am module api
console.log('logicData', this.logicData) --> bonjours I am module Logic
}
Bonus Point
If you want to communicate between the modules for example a action in app-logic which trigger something in api-logic.
So app-logic (module one) to api-logic (module two)
When you specify root: true
it will start to look at the root of the store.
In store/app-logic/actions.js
callPokemonFromAppLogic: ({ dispatch }, id) => {
dispatch('callThePokemonFromApiLogic', id, {root:true});
},
In store/api-logic/actions.js
callThePokemonFromApiLogic: ({ commit }, id) => {
console.log('I make the call here')
axios.get('http://pokeapi.salestock.net/api/v2/pokemon/' + id).then(response => commit('update_pokemon', response.data))
},
In store/api-logic/index.js
add another entry
import actions from './actions';
import getters from './getters';
import mutations from './mutations';
const defaultState = {
appLogicData: 'bonjours I am module Logic',
pokemon: {}
}
const inBrowser = typeof window !== 'undefined';
// if in browser, use pre-fetched state injected by SSR
const state = (inBrowser && window.__INITIAL_STATE__) ? window.__INITIAL_STATE__.page : defaultState;
export default {
state,
actions,
mutations,
getters
}
In store/api-logic/mutations.js
add the pokemon mutation :p
update_pokemon: (state, pokemon) => {
state.pokemon = pokemon
}
Anywhere in the app :
computed: {
...mapGetters({
bidule: 'bidule',
pokemon: 'getPokemon'
})
},
mounted() {
console.log('bidule', this.bidule)
this.callPokemonFromAppLogic('1') --> the call
console.log('the pokemon', this.pokemon.name) --> 'bulbasaur'
},
methods: {
...mapActions({
callPokemonFromAppLogic: 'callPokemonFromAppLogic'
}),
}
At the end your Vue devTool should look like this :)
And Voilà I hope It was clear.
Code example :
https://github.com/CMarzin/nuxt-vuex-modules
add a comment |
I am using nuxt 2.1.0
If you want to have something like this :
In my store/index.js
Make sure you have namespaced: true
import Vuex from 'vuex';
import apiModule from './modules/api-logic';
import appModule from './modules/app-logic';
const createStore = () => {
return new Vuex.Store({
namespaced: true,
modules: {
appLogic: appModule,
api: apiModule
}
});
};
export default createStore
In moduleOne
In my store/api-logic/index.js
import actions from './actions';
import getters from './getters';
import mutations from './mutations';
const defaultState = {
hello: 'salut I am module api'
}
const inBrowser = typeof window !== 'undefined';
// if in browser, use pre-fetched state injected by SSR
const state = (inBrowser && window.__INITIAL_STATE__) ? window.__INITIAL_STATE__.page : defaultState;
export default {
state,
actions,
mutations,
getters
}
In my store/api-logic/getters.js
export default {
getHelloThere: state => state.hello
}
In module Two
In my store/app-logic/index.js
import actions from './actions';
import getters from './getters';
import mutations from './mutations';
const defaultState = {
appLogicData: 'bonjours I am module Logic'
}
const inBrowser = typeof window !== 'undefined';
// if in browser, use pre-fetched state injected by SSR
const state = (inBrowser && window.__INITIAL_STATE__) ? window.__INITIAL_STATE__.page : defaultState;
export default {
state,
actions,
mutations,
getters
}
In my store/app-logic/getters.js
export default {
getAppLogicData: state => state.appLogicData
}
Anywhere in the app
computed: {
...mapGetters({
logicData: 'getAppLogicData',
coucou: 'getHelloThere'
})
},
mounted () {
console.log('coucou', this.coucou) --> salut I am module api
console.log('logicData', this.logicData) --> bonjours I am module Logic
}
Bonus Point
If you want to communicate between the modules for example a action in app-logic which trigger something in api-logic.
So app-logic (module one) to api-logic (module two)
When you specify root: true
it will start to look at the root of the store.
In store/app-logic/actions.js
callPokemonFromAppLogic: ({ dispatch }, id) => {
dispatch('callThePokemonFromApiLogic', id, {root:true});
},
In store/api-logic/actions.js
callThePokemonFromApiLogic: ({ commit }, id) => {
console.log('I make the call here')
axios.get('http://pokeapi.salestock.net/api/v2/pokemon/' + id).then(response => commit('update_pokemon', response.data))
},
In store/api-logic/index.js
add another entry
import actions from './actions';
import getters from './getters';
import mutations from './mutations';
const defaultState = {
appLogicData: 'bonjours I am module Logic',
pokemon: {}
}
const inBrowser = typeof window !== 'undefined';
// if in browser, use pre-fetched state injected by SSR
const state = (inBrowser && window.__INITIAL_STATE__) ? window.__INITIAL_STATE__.page : defaultState;
export default {
state,
actions,
mutations,
getters
}
In store/api-logic/mutations.js
add the pokemon mutation :p
update_pokemon: (state, pokemon) => {
state.pokemon = pokemon
}
Anywhere in the app :
computed: {
...mapGetters({
bidule: 'bidule',
pokemon: 'getPokemon'
})
},
mounted() {
console.log('bidule', this.bidule)
this.callPokemonFromAppLogic('1') --> the call
console.log('the pokemon', this.pokemon.name) --> 'bulbasaur'
},
methods: {
...mapActions({
callPokemonFromAppLogic: 'callPokemonFromAppLogic'
}),
}
At the end your Vue devTool should look like this :)
And Voilà I hope It was clear.
Code example :
https://github.com/CMarzin/nuxt-vuex-modules
I am using nuxt 2.1.0
If you want to have something like this :
In my store/index.js
Make sure you have namespaced: true
import Vuex from 'vuex';
import apiModule from './modules/api-logic';
import appModule from './modules/app-logic';
const createStore = () => {
return new Vuex.Store({
namespaced: true,
modules: {
appLogic: appModule,
api: apiModule
}
});
};
export default createStore
In moduleOne
In my store/api-logic/index.js
import actions from './actions';
import getters from './getters';
import mutations from './mutations';
const defaultState = {
hello: 'salut I am module api'
}
const inBrowser = typeof window !== 'undefined';
// if in browser, use pre-fetched state injected by SSR
const state = (inBrowser && window.__INITIAL_STATE__) ? window.__INITIAL_STATE__.page : defaultState;
export default {
state,
actions,
mutations,
getters
}
In my store/api-logic/getters.js
export default {
getHelloThere: state => state.hello
}
In module Two
In my store/app-logic/index.js
import actions from './actions';
import getters from './getters';
import mutations from './mutations';
const defaultState = {
appLogicData: 'bonjours I am module Logic'
}
const inBrowser = typeof window !== 'undefined';
// if in browser, use pre-fetched state injected by SSR
const state = (inBrowser && window.__INITIAL_STATE__) ? window.__INITIAL_STATE__.page : defaultState;
export default {
state,
actions,
mutations,
getters
}
In my store/app-logic/getters.js
export default {
getAppLogicData: state => state.appLogicData
}
Anywhere in the app
computed: {
...mapGetters({
logicData: 'getAppLogicData',
coucou: 'getHelloThere'
})
},
mounted () {
console.log('coucou', this.coucou) --> salut I am module api
console.log('logicData', this.logicData) --> bonjours I am module Logic
}
Bonus Point
If you want to communicate between the modules for example a action in app-logic which trigger something in api-logic.
So app-logic (module one) to api-logic (module two)
When you specify root: true
it will start to look at the root of the store.
In store/app-logic/actions.js
callPokemonFromAppLogic: ({ dispatch }, id) => {
dispatch('callThePokemonFromApiLogic', id, {root:true});
},
In store/api-logic/actions.js
callThePokemonFromApiLogic: ({ commit }, id) => {
console.log('I make the call here')
axios.get('http://pokeapi.salestock.net/api/v2/pokemon/' + id).then(response => commit('update_pokemon', response.data))
},
In store/api-logic/index.js
add another entry
import actions from './actions';
import getters from './getters';
import mutations from './mutations';
const defaultState = {
appLogicData: 'bonjours I am module Logic',
pokemon: {}
}
const inBrowser = typeof window !== 'undefined';
// if in browser, use pre-fetched state injected by SSR
const state = (inBrowser && window.__INITIAL_STATE__) ? window.__INITIAL_STATE__.page : defaultState;
export default {
state,
actions,
mutations,
getters
}
In store/api-logic/mutations.js
add the pokemon mutation :p
update_pokemon: (state, pokemon) => {
state.pokemon = pokemon
}
Anywhere in the app :
computed: {
...mapGetters({
bidule: 'bidule',
pokemon: 'getPokemon'
})
},
mounted() {
console.log('bidule', this.bidule)
this.callPokemonFromAppLogic('1') --> the call
console.log('the pokemon', this.pokemon.name) --> 'bulbasaur'
},
methods: {
...mapActions({
callPokemonFromAppLogic: 'callPokemonFromAppLogic'
}),
}
At the end your Vue devTool should look like this :)
And Voilà I hope It was clear.
Code example :
https://github.com/CMarzin/nuxt-vuex-modules
edited Dec 3 '18 at 19:02
answered Dec 3 '18 at 18:16
Corentin MarzinCorentin Marzin
664
664
add a comment |
add a comment |
Your issue
Use default
exports in your files to achieve the desired effect (no named exports except in the index.js
)
Example
An example can be found directly in the Nuxt.js test suite (at https://github.com/nuxt/nuxt.js/tree/dev/test/fixtures/basic/store/foo).
If you'd run the basic
fixture and access the /store page you'll see the following result
The "repeated" contents in the module itself just show that the split-up values take priority (otherwise getVal
wouldn't return 10 but 99 and state.val
wouldn't be 4 but 2).
store.vue code:
<template>
<div>
<h1>{{ baz }}</h1>
<br>
<p>{{ $store.state.counter }}</p>
<br>
<h2>{{ getVal }}</h2>
<br>
<h3>{{ getBabVal }}</h3>
</div>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
computed: {
...mapGetters('foo/bar', ['baz']),
...mapGetters('foo/blarg', ['getVal']),
...mapGetters('bab', ['getBabVal'])
}
}
</script>
add a comment |
Your issue
Use default
exports in your files to achieve the desired effect (no named exports except in the index.js
)
Example
An example can be found directly in the Nuxt.js test suite (at https://github.com/nuxt/nuxt.js/tree/dev/test/fixtures/basic/store/foo).
If you'd run the basic
fixture and access the /store page you'll see the following result
The "repeated" contents in the module itself just show that the split-up values take priority (otherwise getVal
wouldn't return 10 but 99 and state.val
wouldn't be 4 but 2).
store.vue code:
<template>
<div>
<h1>{{ baz }}</h1>
<br>
<p>{{ $store.state.counter }}</p>
<br>
<h2>{{ getVal }}</h2>
<br>
<h3>{{ getBabVal }}</h3>
</div>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
computed: {
...mapGetters('foo/bar', ['baz']),
...mapGetters('foo/blarg', ['getVal']),
...mapGetters('bab', ['getBabVal'])
}
}
</script>
add a comment |
Your issue
Use default
exports in your files to achieve the desired effect (no named exports except in the index.js
)
Example
An example can be found directly in the Nuxt.js test suite (at https://github.com/nuxt/nuxt.js/tree/dev/test/fixtures/basic/store/foo).
If you'd run the basic
fixture and access the /store page you'll see the following result
The "repeated" contents in the module itself just show that the split-up values take priority (otherwise getVal
wouldn't return 10 but 99 and state.val
wouldn't be 4 but 2).
store.vue code:
<template>
<div>
<h1>{{ baz }}</h1>
<br>
<p>{{ $store.state.counter }}</p>
<br>
<h2>{{ getVal }}</h2>
<br>
<h3>{{ getBabVal }}</h3>
</div>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
computed: {
...mapGetters('foo/bar', ['baz']),
...mapGetters('foo/blarg', ['getVal']),
...mapGetters('bab', ['getBabVal'])
}
}
</script>
Your issue
Use default
exports in your files to achieve the desired effect (no named exports except in the index.js
)
Example
An example can be found directly in the Nuxt.js test suite (at https://github.com/nuxt/nuxt.js/tree/dev/test/fixtures/basic/store/foo).
If you'd run the basic
fixture and access the /store page you'll see the following result
The "repeated" contents in the module itself just show that the split-up values take priority (otherwise getVal
wouldn't return 10 but 99 and state.val
wouldn't be 4 but 2).
store.vue code:
<template>
<div>
<h1>{{ baz }}</h1>
<br>
<p>{{ $store.state.counter }}</p>
<br>
<h2>{{ getVal }}</h2>
<br>
<h3>{{ getBabVal }}</h3>
</div>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
computed: {
...mapGetters('foo/bar', ['baz']),
...mapGetters('foo/blarg', ['getVal']),
...mapGetters('bab', ['getBabVal'])
}
}
</script>
answered Nov 29 '18 at 16:13
manniLmanniL
3,53742344
3,53742344
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446792%2fnuxt-vuex-how-do-i-break-down-a-vuex-module-into-separate-files%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Good question. Just tried it out and it indeed breaks down my app. Maybe create an issue on cmty.app/nuxt. Seems like either a bug or lack of documentation.
– Imre_G
Nov 23 '18 at 13:01
Example is at github.com/nuxt/nuxt.js/tree/dev/test/fixtures/basic/store/foo/… ;)
– manniL
Nov 23 '18 at 15:30
above answer from @manniL doesn't solve the issue, since the contents of the module directory are simply repeated in the file of the same name in the root of the store
– Guy Harper
Nov 29 '18 at 14:27
Sorry, I got a bit more into detail now ;)
– manniL
Nov 29 '18 at 16:33