lesson 218: store modules

This commit is contained in:
Andreas Zweili 2021-05-05 12:51:01 +02:00
parent 37ba34a22e
commit 55338b1fa3
1 changed files with 31 additions and 15 deletions

View File

@ -1,21 +1,27 @@
import { createStore } from 'vuex'; import { createStore } from 'vuex';
const store = createStore({ const counterModule = {
state() { state() {
return { return {
counter: 0, counter: 0
isLoggedIn: false
}; };
}, },
actions: {
increment(context) {
setTimeout(function() {
context.commit('increment');
}, 2000);
},
increase(context, payload) {
context.commit('increase', payload);
}
},
mutations: { mutations: {
increment(state) { increment(state) {
state.counter = state.counter + 1; state.counter = state.counter + 1;
}, },
increase(state, payload) { increase(state, payload) {
state.counter = state.counter + payload.value; state.counter = state.counter + payload.value;
},
setAuth(state, payload) {
state.isLoggedIn = payload.isAuth;
} }
}, },
getters: { getters: {
@ -31,20 +37,30 @@ const store = createStore({
return 100; return 100;
} }
return finalCounter; return finalCounter;
}, }
}
};
const store = createStore({
modules: {
counter: counterModule
},
state() {
return {
isLoggedIn: false
};
},
mutations: {
setAuth(state, payload) {
state.isLoggedIn = payload.isAuth;
}
},
getters: {
userIsAuthenticated(state) { userIsAuthenticated(state) {
return state.isLoggedIn; return state.isLoggedIn;
} }
}, },
actions: { actions: {
increment(context) {
setTimeout(function() {
context.commit('increment');
}, 2000);
},
increase(context, payload) {
context.commit('increase', payload);
},
login(context) { login(context) {
context.commit('setAuth', { isAuth: true }); context.commit('setAuth', { isAuth: true });
}, },