Getters | Vuex
文章推薦指數: 80 %
Vuex allows us to define "getters" in the store. You can think of them as computed properties for stores. WARNING. As of Vue 3.0, the getter's ... GuideAPIReferenceReleaseNotesv4.xv3.xLanguagesEnglish简体中文日本語PortuguêsGitHubIntroductionWhatisVuex?InstallationGettingStartedCoreConceptsStateGettersProperty-StyleAccessMethod-StyleAccessThemapGettersHelperMutationsActionsModulesAdvancedApplicationStructureCompositionAPIPluginsStrictModeFormHandlingTestingHotReloadingTypeScriptSupportMigrationGuideMigratingto4.0from3.xGetters#TrythislessononScrimbaSometimeswemayneedtocomputederivedstatebasedonstorestate,forexamplefilteringthroughalistofitemsandcountingthem:computed:{ doneTodosCount(){ returnthis.$store.state.todos.filter(todo=>todo.done).length } } Ifmorethanonecomponentneedstomakeuseofthis,wehavetoeitherduplicatethefunction,orextractitintoasharedhelperandimportitinmultipleplaces-botharelessthanideal.Vuexallowsustodefine"getters"inthestore.Youcanthinkofthemascomputedpropertiesforstores.WARNINGAsofVue3.0,thegetter'sresultisnotcachedasthecomputedpropertydoes.ThisisaknownissuethatrequiresVue3.1tobereleased.YoucanlearnmoreatPR#1878.Getterswillreceivethestateastheir1stargument:conststore=createStore({ state:{ todos:[ {id:1,text:'...',done:true}, {id:2,text:'...',done:false} ] }, getters:{ doneTodos(state){ returnstate.todos.filter(todo=>todo.done) } } }) Property-StyleAccess#Thegetterswillbeexposedonthestore.gettersobject,andyouaccessvaluesasproperties:store.getters.doneTodos//->[{id:1,text:'...',done:true}] Getterswillalsoreceiveothergettersasthe2ndargument:getters:{ //... doneTodosCount(state,getters){ returngetters.doneTodos.length } } store.getters.doneTodosCount//->1 Wecannoweasilymakeuseofitinsideanycomponent:computed:{ doneTodosCount(){ returnthis.$store.getters.doneTodosCount } } NotethatgettersaccessedaspropertiesarecachedaspartofVue'sreactivitysystem.Method-StyleAccess#Youcanalsopassargumentstogettersbyreturningafunction.Thisisparticularlyusefulwhenyouwanttoqueryanarrayinthestore:getters:{ //... getTodoById:(state)=>(id)=>{ returnstate.todos.find(todo=>todo.id===id) } } store.getters.getTodoById(2)//->{id:2,text:'...',done:false} Notethatgettersaccessedviamethodswillruneachtimeyoucallthem,andtheresultisnotcached.ThemapGettersHelper#ThemapGettershelpersimplymapsstoregetterstolocalcomputedproperties:import{mapGetters}from'vuex' exportdefault{ //... computed:{ //mixthegettersintocomputedwithobjectspreadoperator ...mapGetters([ 'doneTodosCount', 'anotherGetter', //... ]) } } Ifyouwanttomapagettertoadifferentname,useanobject:...mapGetters({ //map`this.doneCount`to`this.$store.getters.doneTodosCount` doneCount:'doneTodosCount' }) StateMutations
延伸文章資訊
- 1那些關於Vue 的小細節- Computed 中getter 和setter 觸發的 ...
另外,在getter 中,要記得搭配使用return 來把值返回出來。 基本的寫法如下:. 預設只有getter 的computed. new Vue({ ...
- 2Getter | Vuex
Vuex 允许我们在store 中定义“getter”(可以认为是store 的计算属性)。 注意. 从Vue 3.0 开始,getter 的结果不再像计算属性一样会被缓存起来。这是一个 ...
- 3[Vue.js] Vuex 學習筆記(5) - getters 的核心概念- iT 邦幫忙
Getter 為什麼需要使用getters ? 在某些時候我們需要computed store` 中的state ,例如在to do list 內取得完成的數量。 computed: { do...
- 4Mocking a getter in Vuex + Jest | Toucan Toco
You don't want to use the “real” getter, because that would not be a unit test ... Pizza.vue'; co...
- 5[Vue] Vuex 是什麼? 怎麼用? — Getters (3/5) | by itsems - Medium
在沒有getters 的時候,我們需要在template 處理抓回來的state ,才能達到這件事情,先在store.js 裡面加上一個mutations 把api 抓回來的值丟進state ...