1.關于VueX
Vuex是適用于在Vue項目開發(fā)時使用的狀態(tài)管理工具。如果在一個項目開發(fā)中頻繁的使用組件傳參的方式來同步data中的值,一旦項目變得很龐大,管理和維護這些值將是相當棘手的工作。為此,Vue為這些被多個組件頻繁使用的值提供了一個統(tǒng)一管理的工具——Vuex。在具有Vuex的Vue項目中,我們只需要把這些值定義在Vuex中,即可在整個Vue項目的組件中使用。
2.安裝
npm i vuex -s
or
yarn add vuex
在項目的根目錄下新增一個store文件夾,在該文件夾內(nèi)創(chuàng)建index.js
3.使用
初始化store下index.js中的內(nèi)容
import Vue from 'vue'
import Vuex from 'vuex'
//掛載Vuex
Vue.use(Vuex)
//創(chuàng)建VueX對象
const store = new Vuex.Store({
state:{
//存放的鍵值對就是所要管理的狀態(tài)
name:'helloVueX'
}
})
export default store
將store掛載到當前項目的Vue實例當中去
// main.js
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
Vue.config.productionTip = false
new Vue({
el: '#app',
router,
store, //store:store 和router一樣,將我們創(chuàng)建的Vuex實例掛載到這個vue實例中
render: h => h(App)
})
在組件中使用Vuex
<template>
<div id='app'>
name:
<h1>{{ $store.state.name }}</h1>
</div>
</template>
//or
<script>
...,
methods:{
add(){
console.log(this.$store.state.name)
}
},
...
</script>
4.Vuex中的核心內(nèi)容
在Vuex對象中,其實不止有state,還有用來操作state中數(shù)據(jù)的方法集,以及當我們需要對state中的數(shù)據(jù)需要加工的方法集等等成員。
成員列表:
1)state 存放狀態(tài)
2)mutations state成員操作
3)getters 加工state成員給外界
4)actions 異步操作
5)modules 模塊化狀態(tài)管理
4.1Vuex的工作流程

首先,Vue組件如果調(diào)用某個Vuex的方法過程中需要向后端請求時或者說出現(xiàn)異步操作時,需要dispatch Vuex中actions的方法,以保證數(shù)據(jù)的同步。可以說,action的存在就是為了讓mutations中的方法能在異步操作中起作用。
如果沒有異步操作,那么我們就可以直接在組件內(nèi)提交狀態(tài)中的Mutations中自己編寫的方法來達成對state成員的操作。注意,1.3.3節(jié)中有提到,不建議在組件中直接對state中的成員進行操作,這是因為直接修改(例如:this.$store.state.name = 'hello')的話不能被VueDevtools所監(jiān)控到。
最后被修改后的state成員會被渲染到組件的原位置當中去。
4.2 Mutations
更改 Vuex 的 store 中的狀態(tài)的唯一方法是提交 mutation,比如對該數(shù)據(jù)的修改、增加、刪除等等。
mutations 方法都有默認的形參:
([state] ,[payload])
1)state是當前Vuex對象中的state
2)payload是該方法在被調(diào)用時傳遞參數(shù)使用的
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.store({
state:{
name:'helloVueX'
},
mutations:{
//es6語法,等同edit:funcion(){...}
edit(state){
state.name = 'jack'
}
}
})
export default store
而在組件中,我們需要這樣去調(diào)用這個mutation——例如在App.vue的某個method中:
this.$store.commit('edit')
在實際生產(chǎn)過程中,會遇到需要在提交某個mutation時需要攜帶一些參數(shù)給方法使用。
this.$store.commit('edit',15)
//當需要多參提交時,推薦把他們放在一個對象中來提交:
this.$store.commit('edit',{age:15,sex:'男'})
//接收掛載的參數(shù):
...
edit(state,payload){
state.name = 'jack'
console.log(payload) // 15或{age:15,sex:'男'}
}
...
4.3 Getters
類似vue中的計算屬性,getter 的返回值會根據(jù)它的依賴被緩存起來,且只有當它的依賴值發(fā)生了改變才會被重新計算。
Getters中的方法有兩個默認參數(shù)
1)state 當前Vuex對象中的狀態(tài)對象
2)getters 當前getters對象,用于將getters下的其他getter拿來用
getters:{
nameInfo(state){
return "姓名:"+state.name
},
fullInfo(state,getters){
return getters.nameInfo+'年齡:'+state.age
}
}
//組件中使用
this.$store.getters.fullInfo
4.4 Actions
Action 類似于 mutation,不同在于:
1)Action 提交的是 mutation,而不是直接變更狀態(tài)。
2)Action 可以包含任意異步操作。
Actions中的方法有兩個默認參數(shù)
1)context 上下文(相當于箭頭函數(shù)中的this)對象
2)payload 掛載參數(shù)
actions:{
aEdit(context,payload){
setTimeout(()=>{
context.commit('edit',payload)
},2000)
}
}
//組件中使用
this.$store.dispatch('aEdit',{age:15})
4.5 Models
當項目龐大,狀態(tài)非常多時,可以采用模塊化管理模式。Vuex 允許我們將 store 分割成模塊(module)。每個模塊擁有自己的 state、mutation、action、getter、甚至是嵌套子模塊——從上至下進行同樣方式的分割。
const moduleA = {
state: () => ({ ... }),
mutations: { ... },
actions: { ... },
getters: { ... }
}
const moduleB = {
state: () => ({ ... }),
mutations: { ... },
actions: { ... }
}
const store = new Vuex.Store({
modules: {
a: moduleA,
b: moduleB
}
})
//組件內(nèi)調(diào)用模塊a的狀態(tài):
this.$store.state.a
this.$store.commit('editKey')
this.$store.dispatch('aEditKey')