Answer the question
In order to leave comments, you need to log in
How to properly use vuex modules?
Application on Vue
I want to separate the vuex logic into files, that is, modules.
I somehow apparently did it wrong, I get an error
"TypeError: Cannot read property 'state' of undefined"
import Vuex from "vuex";
import Vue from "vue";
import snippets from "./modules/snippets";
Vue.use(Vuex);
export default new Vuex.Store({
modules: {
snippets: snippets
}
});
const snippets = {
namespaced: true,
state: {
filteredSnippets: ["apple", "banana"]
},
mutations: {
SET_FILTERED_SNIPPETS: (state, snippets) => {
state.filteredSnippets = snippets;
}
},
actions: {
FILTER_SNIPPETS({ commit }, snippets) {
commit("SET_FILTERED_SNIPPETS", snippets);
}
}
};
export default snippets;
computed: { ,
...mapState("snippets", ["filteredSnippets"])
},
mounted() {
console.log(this.filteredSnippets);
},
Answer the question
In order to leave comments, you need to log in
and the store itself was added to the app?
I usually do this.
in app.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import StoreData from './store'
const store = new Vuex.Store(StoreData);
const app = new Vue({
el: '#app',
store,
});
import bookings from './bookings'
import events from './events'
export default {
modules: {
bookings,
events
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question