VueJS :: Vuex :: Advanced
PLEASE NOTE!
This is Work-in-Progress so contents are incomplete and possibly wrong. Apologies!
Vuex Modular Setup
Below is an example of how to setup a Modular Vuex store that's easy to test.
First create the src/store/index.js file.
// in src/store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
import token from './modules/token'
Vue.use(Vuex)
export default new Vuex.Store({
modules: {
token
}
})
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
Then create the individual modules in src/store/modules/ directory.
// in src/store/modules/token.js
// State
export const state = {
// custom token
token: null
}
// Actions
export const actions = {
// set token
tokenSet (context, payload) {
context.commit('tokenSet', payload)
}
}
// Getters
export const getters = {
// check if token is set
isTokenSet: state => !state.token
}
// Mutations
export const mutations = {
// set token
tokenSet (state, payload) {
state.token = payload
},
// clear token
tokenClear (state) {
state.token = null
}
}
// Assemble all of the above
export default {
// use namespaced modules: getters['something/isTokenSet']
namespaced: true,
state,
actions,
getters,
mutations
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43