vuex全解說-- mapState, getters帶參數, mapGetters, 展開運算符...

文章推薦指數: 80 %
投票人數:10人

mapGetters 輔助函數與前面提到的 mapState 用法相近,可以簡化程式碼。

文章加密 ; 2019年5月22日星期三 vuex全解說--mapState,getters帶參數,mapGetters,展開運算符..., 這個有很多章,下面的圖片就是從這個的第一章來的 https://jeremysu0131.github.io/Vue-js-Vuex-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-3-%E7%B0%A1%E5%96%AE%E6%87%89%E7%94%A8/ 看到https://jeremysu0131.github.io/Vue-js-Vuex-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-8-actions-%E7%9A%84%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5/ 我們用一個更加實際的例子來調用異步API與分發多重mutations: 在actions中使用async/await 上面这两句的部份看不懂 1.mapState輔助函數 當一個組件需要獲取多個state的時候,如果每次都要宣告為computed會很麻煩,為了解決這個問題Vuex讓我們可以使用mapState輔助函數來幫助我們,將繁瑣的流程簡化。

使用方式 1 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 30 31 32 Output:1,1,3 更簡單的使用方式: 1 2 3 4 computed:mapState([ //將this.count映射為store.state.count 'count' ]) 2.getters使用方式 我們也可以傳參數到getters來取得返回結果,這是非常便利的方式查詢store中的陣列。

store 1 2 3 4 5 6 7 8 9 10 11 12 13 conststore=newVuex.Store({ state:{ todos:[ {id:1,text:'...',done:true}, {id:2,text:'...',done:false} ] }, getters:{ getTodoById:(state)=>(id)=>{ returnstate.todos.find(todo=>todo.id===id) } } }) component 1 2 3 4 5 6 7 computed:{ getTodoById(){ returnthis.$store.getters.getTodoById(2); } } //{"id":2,"text":"...","done":false} 3.mapGetters輔助函數 mapGetters 輔助函數與前面提到的 mapState 用法相近,可以簡化程式碼。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Output:[{“id”:1,“text”:“…”,“done”:true}],1 如果我們要將getters屬性取另外一個名稱,可以用物件的方式: 新名稱:‘getters屬性名稱’ 1 2 3 4 computed:mapGetters({ //映射`this.doneCount`為`store.getters.doneTodosCount` doneCount:'doneTodosCount' }), 4.展開運算符(SpreadOperator) 上述mapState和mapGetters都有組件或陣列的兩種寫法,這兩種寫法可以混合使用,只要搭配展開運算符...即可! https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Operators/Spread_syntax uex 1 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 conststore=newVuex.Store({ state:{ count:0, todos:[{ id:1, text:'...', done:true }, { id:2, text:'...', done:false } ] }, getters:{ doneTodos:state=>{ returnstate.todos.filter(todo=>todo.done) }, doneTodosCount:(state,getters)=>{ returngetters.doneTodos.length }, getTodoById:(state)=>(id)=>{ returnstate.todos.find(todo=>todo.id===id) } } }); component 1 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 5.Mutations遵守Vue的響應規則 因為Vuex中的store的state是響應式的,那麼當我們變更state時,監控state的組件也會自動更新,這也意味著Vuex中的mutations也需要與使用Vue一樣遵守下列的注意事項: 最好提前在store中初始化好所有所需的屬性。

當新增新的屬性到物件時,你應該: 使用 UseVue.set(obj,'newProp',123) 或是以新物件替換舊物件,例如我們可以利用物件展開運算符來寫: 1 state.obj={...state.obj,newProp:123} 6.使用常量提供mutations類型(這個還沒有懂) 在各種Flux實現中,使用常量用於mutations類型是一種常見的模式,這樣可以使IDE中的各式linter工具發揮作用,並將所有常量放在一個文件中,讓你的協作者可以快速瀏覽整個應用程序中可能發生的變化。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 //mutation-types.js exportconstSOME_MUTATION='SOME_MUTATION' //store.js importVuexfrom'vuex' import{SOME_MUTATION}from'./mutation-types' conststore=newVuex.Store({ state:{...}, mutations:{ //我們可以使用ES2015風格的計算屬性命名功能來使用一個常量作為函數名 [SOME_MUTATION](state){ //mutatestate } } }) 用不用常量取決於自己,但在需要多人協作的大型項目中使用會很有幫助。

7.在組件中提交mutations 我們可以在組件中使用 this.$store.commit('xxx') ,提交mutations或者使用 mapMutations 輔助函數將組件中的methods映射為 store.commit 來調用。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 exportdefault{ //... methods:{ ...mapMutations([ 'increment',//將`this.increment()`映射為`this.$store.commit('increment')` //`mapMutations`也支援payloads: 'incrementBy'//將`this.incrementBy(amount)`映射為`this.$store.commit('incrementBy',amount)` ]), ...mapMutations({ add:'increment'//將`this.add()`映射為`this.$store.commit('increment')` }) } } 只是將`this.increment()`映射為`this.$store.commit('increment')`,不能直接把它拿去@click="increment",又不能寫一樣的名稱如下 methods:{ getapi(){ this.getapi()//這樣會執行失敗,因為Errorinv-onhandler:"RangeError:Maximumcallstacksizeexceeded" } } 8.Actions簡化程式碼 在實際情況中,我們常會使用ES2015中的 參數解構 來簡化程式碼: 原來 actions:{ increment(context){ context.commit("increment"); } } 簡化後 1 2 3 4 5 actions:{ increment({commit}){ commit('increment') } } 相似的简写法还有dispatch,state,例子如下 actions:{ //... actionB({dispatch,commit}){ returndispatch('actionA').then(()=>{ commit('someOtherMutation') }) } } actions是用在异步执行,以下说明一些例子(配合newPromise) state:{ count:10, }, mutations:{ increment(state,n=3){ state.count+=n; } }, actions:{ increment(context){ context.commit('increment'); }, asyncIncrement(context){ returnnewPromise(resolve=>{ setTimeout(()=>{ context.commit('increment'); resolve(); },3000) }) }, incrementAsync({commit}){ setTimeout(()=>{ commit('increment') },1000) } }



請為這篇文章評分?