VueJS - Vuex
Vuex
Vuex簡介
- 概念:
專門在Vue中實現集中式數據(狀態)管理的一個Vue插件,對Vue應用中多個組件的共享數據(狀態)進行集中式的管理(讀/寫),也是一種組件間通信的方式,適用於任意組件中的通信
- 什麼時候使用Vuex
a. 多個組件依賴於同一數據
b. 來自不同組件的行為需要變更同一數據
Vue工作原理

Vue Components:組件,發送操作- 通過調用
dispatch()方法向Vuex傳遞要執行的操作1
2
3// 參數一、要進行的操作
// 參數二、操作所需的數據
dispatch('add', 2)
- 通過調用
Actions:一個對象,用於保存動作、行為- 裏面存有操作所對應的 方法
add:function(){...} - 在函數裡調用
commit()方法向Mutations傳遞數據1
2
3// 參數一、要進行的操作
// 參數二、操作所需的數據
commit('add', 2)
- 裏面存有操作所對應的 方法
Mutations:一個對象,用於修改、更新數據- 裏面存有操作所對應的 初始化的State 和 操作的數據
- 通過以下方式更改保存在State中的數據,進行數據的更新
1
state.sum += 2
State:一個對象,用於保存數據- 當數據更新完後,Vuex會重新解析和渲染頁面上的動態數據
搭建Vuex環境
- 創建
store:- 創建文件:
src/store/index.js1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22// 引入Vue核心庫
import Vue from 'vue'
// 引入Vuex
import Vuex from 'vuex'
// 應用Vuex插件
Vue.use(Vuex)
// 準備actions,用於響應組件中的動作
const actions = {}
// 準備mutations,用於操作數據(state)
const mutations = {}
// 準備state,用於存儲數據
const state = {}
// 創建並暴露store
export default new Vuex.Store({
actions,
mutations,
state
})
- 創建文件:
- 配置
store:- 在
main.js中創建vm時傳入store配置項1
2
3
4
5
6
7// 引入store
import store from './store/index.js'
new Vue({
// 配置store
store,
})
- 在
Vuex基本使用
- 初始化數據,配置
actions、配置mutations,操作文件store.js1
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// 引入Vue
import Vue from 'vue'
// 引入Vuex
import Vuex from 'vuex'
// 使用vuex插件
Vue.use(Vuex)
const actions = {
add(context,value){
context.commit('ADD', value)
},
}
const mutations = {
ADD(state,value){
state.sum += value
},
}
// 初始化數據
const state = {
sum:0,
}
export default new Vuex.Store({
actions,
mutations,
state,
})
- 組件中讀取vuex的數據:
1
$store.state.sum
- 組件中修改vuex中的數據:
1
2$store.dispatch('actionName', data) // 透過actions修改數據
$store.commit('mutationsName', data) // 跳過actions,直接聯繫mutations修改- 備註:若沒有網絡請求或其他邏輯,則可直接使用
commit修改數據
- 備註:若沒有網絡請求或其他邏輯,則可直接使用
getters的使用
- 概念:當
state中的數據需要進行加工後再使用,可以使用getters加工
- 在
store.js中追加getters配置1
2
3
4
5
6
7
8
9
10
11
12...
const getters = {
tenTimes(state){
return state.sum * 10
}
}
export default new Vuex.Store({
...
getters,
})
- 組件中讀取數據:
1
$store.getters.tenTimes
四種map方法的使用
mapState方法:用於映射state中的數據為計算屬性1
2
3
4
5
6
7computed:{
// 借助 mapState 生成計算屬性,從state中讀取數據(對象寫法)
...mapState({a:'sum', b:'school', c:'major'}),
// 借助 mapState 生成計算屬性,從state中讀取數據(數組寫法)
...mapState(['sum', 'school', 'major']),
}
mapGetters方法:用於映射getters中的數據為計算屬性1
2
3
4
5
6
7computed:{
// 借助 mapGetters 生成計算屬性,從getters中讀取數據(對象寫法)
...mapGetters({tenTimes:'tenTimes'})
// 借助 mapGetters 生成計算屬性,從getters中讀取數據(數組寫法)
...mapGetters(['tenTimes'])
}
mapActions方法:用於生成與actions對話的方法,即包含$store.dispatch(xxx)的函數1
2
3
4
5
6
7computed:{
// 借助 mapActions 生成對應的方法,方法中會調用dispatch去聯繫actions(對象的寫法)
...mapActions({incrementOdd:'addOdd', incrementWait:'addWait'}),
// 借助 mapActions 生成對應的方法,方法中會調用dispatch去聯繫actions(對象的寫法)
...mapActions(['addOdd', 'addWait']),
}
mapMutations方法:用於生成與mutations對話的方法,即包含$store.commit(xxx)的函數1
2
3
4
5
6
7computed:{
// 借助 mapMutations 生成對應的方法,方法中會調用commit去聯繫mutations(對象的寫法)
...mapMutations({increment:'ADD', decrement:'MINUS'}),
// 借助 mapMutations 生成對應的方法,方法中會調用commit去聯繫mutations(數組的寫法)
...mapMutations(['ADD', 'MINUS']),
}
- 備註:
mapActions與mapMutations使用時,若需要傳遞參數,需要在模板中綁定事件時傳遞參數,否則參數是默認的事件對象
Vuex模塊化及命名空間
- 目的:讓代碼更好維護,讓更多數據分類明確
- 修改
store.js1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20const countAbout = {
namespaced:true // 開啟命名空間
state:{...},
actions:{...},
mutations:{...},
getters:{...}
}
const personAbout = {
namespaced:true // 開啟命名空間
state:{...},
actions:{...},
mutations:{...},
getters:{...}
}
export default new Vuex.Store({
modules:{
countAbout,
personAbout,
}
})
- 開啟命名空間後,組件中讀取
state數據:1
2
3
4// 方式一:直接讀取
this.$store.state.countAbout.xxx
// 方式二:借助mapState讀取
...mapState('countAbout', ['xxx'])
- 開啟命名空間後,組件中讀取
getters數據:1
2
3
4// 方式一:直接讀取
this.$store.getters['countAbout/xxx']
// 方式二:借助mapGetters讀取
...mapGetters('countAbout', ['xxx'])
- 開啟命名空間後,組件中調用
dispatch:1
2
3
4// 方式一:直接讀取
this.$store.dispatch('countAbout/xxx', value)
// 方式二:借助mapActions讀取
...mapActions('countAbout', ['xxx'])
- 開啟命名空間後,組件中調用
commit:1
2
3
4// 方式一:直接讀取
this.$store.commit('countAbout/XXX', value)
// 方式二:借助mapMutations讀取
...mapMutations('countAbout', ['XXX'])
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.


