vue3.x中vue4.x如何使用modules模块化

store/modules/a.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
export default {
namespaced: true,
state: () => ({
}),
getters: {
},
actions: {
async getData ({state, commit, dispatch, rootState}) {
await ajax()
...
}
},
mutations: {
},
}

store/modules/index.js

1
2
3
4
5
6
7
import a from './a.js'
...

export default {
a,
...
}

store/index.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { createStore } from 'vuex'
import modules from './modules'

const store = createStore({
modules, // 模块

state: {}, // 全局

mutations: { }, // 全局

actions: { }, // 全局

getters: { } // 全局
})

export default store

调用

1
2
3
4
5
6
7
8
import { onMounted } from 'vue'
import { useStore } from 'vuex'

const store = useStore()

onMounted(() => {
store.dispatch('a/getData')
})