? ? ? 使用過vue的人都知道vue實(shí)例有一個(gè)選項(xiàng)這么個(gè)東西,什么是選項(xiàng)呢.比如一個(gè)vue實(shí)例里的el,data,computed,mounted,methods等等.所以vuex用一個(gè)叫store的實(shí)例.他可以當(dāng)做vue實(shí)例的一個(gè)選項(xiàng)去使用,和vue-router一樣使用前,調(diào)用Vue.use(vue-router) Vue.use(Vuex).
如果在vue實(shí)例里選項(xiàng)的鍵值相同的話可以使用簡(jiǎn)寫
store實(shí)例與vue實(shí)例可以對(duì)應(yīng)的去理解
store ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? vm
state ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? data
mutation ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?methods
getters ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? computed
一旦在跟實(shí)例里使用store選項(xiàng),那么下邊所有子組件都可以使用store里的state,getters.
而store里的Actions和mutations是對(duì)store實(shí)例狀態(tài)的處理,其中Actions不會(huì)直接去改變狀態(tài)state,而是去分發(fā),調(diào)度,提交一個(gè)事件類型,狀態(tài)改變只發(fā)生在mutations里
? ? ? ? actions和mutations里的事件可以不使用[] 可以直接寫USER_SINGIN,其中Actions中事件的第一個(gè)參數(shù)是content,它具有和store相同的屬性和方法但不是store實(shí)例本身,后面的參數(shù)就是用戶操作時(shí)事件傳遞的參數(shù),像{ commit ,dispatch }寫法是一種簡(jiǎn)寫的方式,意思是從context獲取commit和dispatch兩個(gè)方法,如果不這樣寫可以用context.dispatch或者context.commit方法,其中dispatch和commit在分發(fā)事件上有一定的區(qū)別.mutations時(shí)間里的第一個(gè)參數(shù)是state,它可以訪問store實(shí)例里的所有state并且可以改變他們從而使頁面UI發(fā)生改變.
? ? ? getters更傾向于寫一些過濾store里state的方法,
? ? ?在子組件里可以通過this.$store 來獲取store下邊的state和getters對(duì)象里的方法
? ? 當(dāng)然store實(shí)例選項(xiàng)有對(duì)應(yīng)的輔助函數(shù)import { mapActions ,mapState ,mapMutations} from 'vuex';
? 在子組件里你可以這樣使用
第一次寫這種東西,不知道寫的亂不亂,如有高人指點(diǎn),感激不盡
可以詳談----微信:::18510954161