一、使用Vuex的目的
實(shí)現(xiàn)多組件狀態(tài)管理。多個(gè)組件之間需要數(shù)據(jù)共享時(shí),Vuex是個(gè)很好的幫手哦
二、Vuex 的五大核心
其中state和mutation是必須的,其他可根據(jù)需求來加
1.state
負(fù)責(zé)狀態(tài)管理,類似于vue中的data,用于初始化數(shù)據(jù)
2.mutation
專用于修改state中的數(shù)據(jù),通過commit觸發(fā)
3.action
可以處理異步,通過dispatch觸發(fā),不能直接修改state,首先在組件中通過dispatch觸發(fā)action,然后在action函數(shù)內(nèi)部commit觸發(fā)mutation,通過mutation修改state狀態(tài)值
4.getter
Vuex中的計(jì)算屬性,相當(dāng)于vue中的computed,依賴于state狀態(tài)值,狀態(tài)值一旦改變,getter會(huì)重新計(jì)算,也就是說,當(dāng)一個(gè)數(shù)據(jù)依賴于另一個(gè)數(shù)據(jù)發(fā)生變化時(shí),就要使用getter
5.module
模塊化管理
三、Vuex 怎么用
1.首先當(dāng)然要安裝啦 (假設(shè)你已經(jīng)安裝了vue)
1
|
npm install vuex |
2.引入并使用Vuex
1
2
3
|
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) |
3.實(shí)例化一個(gè)Vuex對(duì)象--Store狀態(tài)機(jī) 并拋出
1
2
3
|
const store = new Vuex.Store({}) export defaul store |
4.在main.js中引入并注入Vuex
1
2
3
4
5
6
7
|
import Vue from 'vue' import App from './App.vue' import store from './store' new Vue({ render: h => h(App), store }).$mount( '#app' ) |
5.通過this.$store訪問Vuex的數(shù)據(jù)
到此,Vuex已準(zhǔn)備完成,接下來就根據(jù)自己需要加入數(shù)據(jù)吧~
四、示例代碼
1.首先準(zhǔn)備至少兩個(gè)組件,不然怎么數(shù)據(jù)共享呢
1
2
3
4
5
6
7
8
9
10
11
|
< template > < div > 我是組件1 </ div > </ template > < script > export default { } </ script > < style scoped> </ style > |
1
2
3
4
5
6
7
8
9
10
11
|
< template > < div > 我是組件2 </ div > </ template > < script > export default { } </ script > < style scoped> </ style > |
2.在store.js中寫Vuex相關(guān)代碼
1
2
3
4
5
6
7
8
9
10
11
|
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ state: {}, //狀態(tài)管理 mutations: {}, //修改state actions: {}, //異步操作 getters:{}, //計(jì)算屬性 modules: {} //模塊 }) export default store |
注:別忘了在main.js里注入store呀(參考上面第三大點(diǎn)的第4小點(diǎn))
3.假設(shè)咱們現(xiàn)在需要共享一個(gè)數(shù)據(jù)data,初始值為0
1
2
3
4
5
6
7
|
··· const store = new Vuex.Store({ state: { data:0 } }) ··· |
4.組件1和組件2都可以通過$store拿到data
1
2
3
4
|
<div> 我是組件1 {{ this .$store.state.data}} //瀏覽器中此處會(huì)顯示data的值 </div> |
1
2
3
4
|
<div> 我是組件2 {{ this .$store.state.data}} //瀏覽器中此處會(huì)顯示data的值 </div> |
5.那么怎么修改data呢,這時(shí)候就需要mutation出馬了
mutation實(shí)質(zhì)是一個(gè)函數(shù),接收state和調(diào)用時(shí)傳來的params參數(shù)
1
2
3
4
5
|
mutations: { changeDataMut(state,params){ state.data = params } } |
6.mutation有了,接下來就需要在組件中調(diào)用它,記得要用commit觸發(fā)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<template> <div> 我是組件1 <button @click= 'changeData' >改數(shù)據(jù)</button> </div> </template> <script> export default { data(){ return { changeData(){ // 通過commit 觸發(fā) mutation 并傳參 this .$store.commit( 'changeDataMut' ,10) //此時(shí)組件1和組件2中data都是10啦 } } } } </script> |
7.action怎么用?
- action也是函數(shù)。
- 前面已經(jīng)說到,action不能直接修改state,首先要在組件中通過dispatch觸發(fā)action,然后在action函數(shù)內(nèi)部commit觸發(fā)mutation,通過mutation修改state狀態(tài)值。
- 注意action的參數(shù)
1
2
3
4
5
6
|
actions: { changeDataAct(context,params){ //context是一個(gè)對(duì)象,從它里面把咱們需要的commit方法解構(gòu)出來 let {commit} = context commit( 'changeDataMut' ,params) } } |
這時(shí)候可以在組件中觸發(fā)action了,注意使用dispatch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<template> <div> 我是組件2 <button @click= 'changeData' >改數(shù)據(jù)</button> </div> </template> <script> export default { data(){ return { changeData(){ // 通過dispatch 觸發(fā) action 并傳參 this .$store.dispatch( 'changeDataAct' ,100) //此時(shí)data就變成100啦,并且組件1和組件2是同步的 } } } } </script> |
8.getter的用法
- getter 計(jì)算屬性 ,依賴于state值,需要return
- 現(xiàn)在咱們假設(shè)一個(gè)值double,它是data的兩倍,并且會(huì)隨著data發(fā)生改變
1
2
3
4
5
|
getters:{ doubleGet(state){ return state.data*2 } } |
- 我們可以在組件中使用它了
1
2
3
4
|
< div > 我是組件2 {{this.$store.getters.doubleGet}} </ div > |
以上就是vuex的使用步驟的詳細(xì)內(nèi)容,更多關(guān)于vuex 使用的資料請(qǐng)關(guān)注服務(wù)器之家其它相關(guān)文章!
原文鏈接:https://segmentfault.com/a/1190000038805329