1. Vuex 概述
1.1 組件之間共享數據的方式
- 父向子傳值: v-bind 屬性綁定
- 子向父傳值: v-on 事件綁定
- 兄弟組件之間共享數據: EventBus
- $on: 接收數據的那個組件
- $emit: 發送數據的那個組件
1.2 Vuex是什么
Vuex: 是實現組件全局狀態(數據)管理的一種機制,可以方便的實現組件之間數據的共享
1.3 使用Vuex統一管理狀態的好處
- 能夠在vuex中集中管理共享的數據,易于開發和后期維護
- 能夠高效地實現組件之間地數據共享,提高開發效率
- 存儲在vuex中的數據都是響應式的,能夠實時保持數據與頁面的同步
[vuex中存儲的數據] : 一般情況下,只有組件之間共享的數據,才有必存儲到vuex中;對于組件中私有數據,依舊存儲在組件自身的data即可
2. Vuex的基本使用
- 安裝vuex依賴包
npm install vuex --save
- 導入vuex包
import Vuex form 'vuex'
Vue.use(Vuex)
- 創建store對象
const store = new Vuex.store({state: { count: 0 }
})
- 將store對象掛在到vue實例中
new Vue({el: "#app",render: h => h(app),router,store
})
3. Vuex的核心概念
3.1 State
State提供唯一的公共數據源,所有共享的數據都要統一放到Store的State中進行存儲
// 創建store數據源,提供唯一公共數據
const store = new Vuex.Store({state: { count: 0 }
})
組件訪問State中數據的第一種方式:
this.$store.state.全局數據名稱
組件訪問State中數據的第二種方式
// 1. 從 vuex中按需導入mapState函數
import { mapState } from 'vuex'// 2. 將全局數據,映射為當前組件的計算屬性
computed: {...mapState(['count'])
}
3.2 Mutation
- Mutation用于變更Store中的數據,不推薦以下做法改變全局數據(
this.$store.state.count
)
<template><div><h3>當前最新的count值為: {{$store.state.count}} </h3><button @click="btnHandler1">+1</button></div>
</template>
<script>
export default {data() {return {}},methods: {btnHandler1() {this.$store.state.count++}}
}
</script>
- 只能通過mutation變更store數據,不可以直接操作Store中的數據
- 這種方式可以集中監控所有數據的變化
3.2.1 $store.commit
// 定義Mutation
const store = new Vuex.Store({state: {count: 0},mutations: {add(state) {// 變更狀態state.count++}}
})// 觸發mutation
methods:{handle1() {this.$store.commit('add')}
}
可以在觸發mutations時傳遞參數:
// 定義mutation
const store = new Vuex.Store({state: {count: 0},mutations: {addN(state, step){// 變更狀態state.count += step}}
})// 觸發mutation
methods:{handle2(){this.$store.commit('addN', 3)}
}
3.2.2 mapMutations
// 1. 從vuex中按需導入mapMutations函數
import { mapMutations } from 'vuex'// 2. 將指定的mutations函數,映射為當前組件的methods函數
methods:{...mapMutations(['add','addN'])
}
3.3 Action
3.3.1 $store.dispatch
- Action用于處理異步任務
- 如果通過異步操作變更數據,必須通過Action,而不能使用Mutation,但是在Action中還是要通過觸發Mutation的方式間接變更數據
// 定義action
const store = new Vuex.Store({mutations: {add(state) {state.count++}},actions: {addAsync(context) {setTimeout(()=>{context.commit('add')}, 1000)}}
})// 觸發action
methods{handle(){this.$store.dispatch('addAsync')}
}
- 攜帶參數的actions異步任務
// 定義Action
const store = new Vuex.Store({mutations: {addN(state, step) {state.count += step}},actions: {addNAsync(context, step){setTimeout(()=>{context.commit('addN', step)}, 1000)}}
})
// 觸發Action
methods:{handle(){this.$store.dispatch('addNAsync', 5)}
}
3.3.2 mapActions
// 1. 從 vuex中按需導入 mapActions函數
import { mapActions } from 'vuex'// 2. 在methods中使用
methods:{...mapActions['addAsync','addNAsync']
}
3.4 Getter
3.4.1 $store.getters
- 用于對Store中的數據進行處理形成新的數據
- 可以對Store中已有的數據加工處理之后形成新的數據,類似Vue的計算屬性
- Store中數據發生變化,Getter的數據也會跟著變化
const store = new Vuex.Store({state: {count: 0},getters:{showNum: state => {return '當前最新的數量是['+ state.count +']'}}
})
<!-- 調用 -->
<template><h3>{{$store.getters.showNum}}</h3>
</template>
3.4.2 mapGetters
import { mapGetters } from 'vuex'computed: {...mapGetters(['showNum'])
}