Vuex使用手册
安装
npm install vuex@next --save
开始
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| import { createApp } from 'vue' import { createStore } from 'vuex'
const store = createStore({ state () { return { count: 0 } }, mutations: { increment (state) { state.count++ } } })
const app = createApp({ })
app.use(store)
|
Api
state
vue 组件使用state
1 2 3 4 5
| computed: { count () { return this.$store.state.count } }
|
借助mapState 辅助函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import { mapState } from 'vuex'
export default { computed: mapState({ count: state => state.count,
countAlias: 'count',
countPlusLocalState (state) { return state.count + this.localCount } }) }
|
getter
派生
1 2 3 4 5 6 7 8 9 10 11 12 13
| const store = createStore({ state: { todos: [ { id: 1, text: '...', done: true }, { id: 2, text: '...', done: false } ] }, getters: { doneTodos: (state) => { return state.todos.filter(todo => todo.done) } } })
|
vue组件使用getter
借助 mapGetters
1 2 3 4 5 6 7 8 9 10 11 12 13
| import { mapGetters } from 'vuex'
export default { computed: { ...mapGetters([ 'doneTodosCount', 'anotherGetter', ]) } }
|
映射name
1 2 3
| ...mapGetters({ doneCount: 'doneTodosCount' })
|
mutation
change更新事件 只能处理同步
1 2 3 4 5 6 7 8 9 10
| const store = createStore({ state: { count: 1 }, mutations: { increment (state,payload) { state.count+=payload; } } })
|
vue组件触发
1
| store.commit('increment',10);
|
借助mapMutation辅助函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import { mapMutations } from 'vuex'
export default { methods: { ...mapMutations([ 'increment',
'incrementBy' ]), ...mapMutations({ add: 'increment' }) } }
|
action
change更新事件 处理异步
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| const store = createStore({ state: { count: 0 }, mutations: { increment (state) { state.count++ } }, actions: { increment ({ commit }) { commit('increment') }, incrementAsync ({ commit }) { setTimeout(() => { commit('increment') }, 1000) } } })
|
vue 组件中使用action
1
| store.dispatch('increment')
|
借助mapActions辅助函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import { mapActions } from 'vuex'
export default { methods: { ...mapActions([ 'increment',
'incrementBy' ]), ...mapActions({ add: 'increment' }) } }
|
组合action
1 2 3 4 5 6 7 8 9
| actions: { async actionA ({ commit }) { commit('gotData', await getData()) }, async actionB ({ dispatch, commit }) { await dispatch('actionA') commit('gotOtherData', await getOtherData()) } }
|
module
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| const moduleA = { state: () => ({ ... }), mutations: { ... }, actions: { ... }, getters: { ... } }
const moduleB = { state: () => ({ ... }), mutations: { ... }, actions: { ... } }
const store = createStore({ modules: { a: moduleA, b: moduleB } })
store.state.a store.state.b
|
vue组件使用
1 2 3 4 5 6 7 8 9 10 11 12
| computed: { ...mapState('some/nested/module', { a: state => state.a, b: state => state.b }) }, methods: { ...mapActions('some/nested/module', [ 'foo', 'bar' ]) }
|
或
1 2 3 4 5 6 7 8 9 10 11 12
| computed: { ...mapState({ a: state => state.some.nested.module.a, b: state => state.some.nested.module.b }) }, methods: { ...mapActions([ 'some/nested/module/foo', 'some/nested/module/bar' ]) }
|