How to use getters in Vuex - Educative.io
文章推薦指數: 80 %
getters to the rescue. This is where Vuex getters become handy. We implement our getter function in the Vuex store and call it in all the components we need it. SolutionsDevelopmentTeamsOnboard,Upskill,RetainDevelopersLearnnewtechnologiesProductsLearningforTeamsSuperchargeyourengineeringteamLearningforIndividualsWorldclasscoursesOnboardingOnboardnewhiresfasterAssessmentsMeasureyourSkillScorePersonalizedLearningPlansPersonalizedPlansforyourgoalsProjectsBuildrealworldapplicationsPricingForIndividualsStayaheadofthecurveForTeamsTailoredforyourteamCoursesLogInJoinforfreeashotofdevknowledgeRELATEDTAGSHowtousegettersinVuexVuexisthestatemanagementlibraryforVuejs. Vuexmakesuseofacentralstateitusestomanageitsstoredata. Ifweintendtoaccessthisstateobjectinacomponent,wecouldaccessitassuch: computed:{ approvedUsers(){ returnthis.$store.state.users.filter(user=>user.approved) } } However,supposingweintendtousethismethodinothercomponents,wewouldeitherneedtoextractitintoahelperfunctionandimportitintoallthosecomponentsorfollowthetraditionalwayofcopyingandpasting. getterstotherescue ThisiswhereVuexgettersbecomehandy.WeimplementourgetterfunctionintheVuexstoreandcallitinallthecomponentsweneedit. conststore=newVuex.Store({ state:{ users:[ {id:1,approved:false,username:"Joe"}, {id:2,approved:true,username:"Tim"} ] }, getters:{ approvedUsers:state=>{ returnstate.users.filter(user=>user.approved) } } }) Accessingitinacomponent Toaccessitinthecomponentofourchoice,wecallthe$this.store.gettershelper: computed:{ approvedUsers(){ returnthis.$store.getters.approvedUsers } } UsingthemapGettershelper WecanalsousethemapStatehelper,whichallowsustogetridofthemany$this.store.gettersinthecomputedproperty. import{mapGetters}from'vuex' exportdefault{ computed:{ ...mapGetters([ 'approvedUsers' ]) } } Thanksforreading! RELATEDTAGSRELATEDCOURSESViewallCoursesKeepExploringRelatedCoursesLearnin-demandtechskillsinhalfthetimeSOLUTIONSEducativeforBusinessEducativeforIndividualsEducativeforHR/recruitingEducativeforBootcampsPRODUCTSEducativeLearningEducativeOnboardingEducativeSkillAssessmentsPricingForindividualsForTeamsRESOURCESEducativeBlogEdpressoCONTRIBUTEBecomeanAuthorBecomeanAffiliateBecomeaContributorLEGALPrivacyPolicyCookieSettingsTermsofServiceBusinessTermsofServiceABOUTUSOurTeamCareersHiringMORECourseCatalogEarlyAccessCoursesFreeTrialsCodingInterview.comPressContactUsSOLUTIONSEducativeforBusinessEducativeforIndividualsEducativeforHR/recruitingEducativeforBootcampsPricingForindividualsForTeamsLEGALPrivacyPolicyCookieSettingsTermsofServiceBusinessTermsofServicePRODUCTSEducativeLearningEducativeOnboardingEducativeSkillAssessmentsCONTRIBUTEBecomeanAuthorBecomeanAffiliateBecomeaContributorABOUTUSOurTeamCareersHiringRESOURCESEducativeBlogEdpressoMORECourseCatalogEarlyAccessCoursesFreeTrialsCodingInterview.comPressContactUsSOLUTIONSEducativeforBusinessEducativeforIndividualsEducativeforHR/recruitingEducativeforBootcampsRESOURCESEducativeBlogEdpressoCONTRIBUTEBecomeanAuthorBecomeanAffiliateBecomeaContributorABOUTUSOurTeamCareersHiringPRODUCTSEducativeLearningEducativeOnboardingEducativeSkillAssessmentsPricingForindividualsForTeamsLEGALPrivacyPolicyCookieSettingsTermsofServiceBusinessTermsofServiceMORECourseCatalogEarlyAccessCoursesFreeTrialsCodingInterview.comPressContactUsCopyright©2022Educative,Inc.Allrightsreserved.
延伸文章資訊
- 1Getter | Vuex
Vuex 允许我们在store 中定义“getter”(可以认为是store 的计算属性)。 注意. 从Vue 3.0 开始,getter 的结果不再像计算属性一样会被缓存起来。
- 2Vuex getters - Laracasts
Is it possible to pass extra arguments to Vuex getters? For example, I want to make a getter that...
- 3[Vue] Vuex 是什麼? 怎麼用? — Getters (3/5) | by itsems - Medium
Getters 情境範例. Getters 簡單說就是可以把state 處理過後再丟出去的人,接續上中篇的範例假設情境: 我想要在頁面上show 出5 個random 的人,還要 ...
- 4vuex 進階教學:Module、Getter
接續上回〈vuex 教學:vue 的狀態管理工具〉教學後,這次要延伸筆記Module、Getter 的使用,也包含較進階的用法,如果已經學完上一版的教學, ...
- 5Dynamic Vuex Getters - A Vue.js Lesson From ... - Vue School
In this Vue.js tutorial, we'll look into how we can use higher order functions to create dynamic ...