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
延伸文章資訊
- 1How to set up Pinia getter in Vue 3 Composition API - Stack ...
- 2Getters | Vuex
Vuex allows us to define "getters" in the store. You can think of them as computed properties for...
- 3[Vue.js] Vuex 學習筆記(5) - getters 的核心概念- iT 邦幫忙
Getter 為什麼需要使用getters ? 在某些時候我們需要computed store` 中的state ,例如在to do list 內取得完成的數量。 computed: { do...
- 4【Vue学习总结】26.Vuex的重要属性之Getter、Action、Module
我们修改一下上一篇编写的store.js,增加一个Getter:. import Vue from 'vue'.
- 5vuex 進階教學:Module、Getter
接續上回〈vuex 教學:vue 的狀態管理工具〉教學後,這次要延伸筆記Module、Getter 的使用,也包含較進階的用法,如果已經學完上一版的教學, ...