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 的结果不再像计算属性一样会被缓存起来。
- 2學習Vuex 的Module 與Getter - Medium
在前面有提到: 可以將store 物件切分多個模組,並讓每個模組都擁有自己的state, mutation, action, getter,所以將資料夾的結構建立如圖:. 透過 modules.
- 3[Vue] Vuex 是什麼? 怎麼用? — Getters (3/5) | by itsems - Medium
Getters 情境範例. Getters 簡單說就是可以把state 處理過後再丟出去的人,接續上中篇的範例假設情境: 我想要在頁面上show 出5 個random 的人,還要 ...
- 4Vuex getters - Laracasts
Is it possible to pass extra arguments to Vuex getters? For example, I want to make a getter that...
- 5How to use getters in Vuex - Educative.io
getters to the rescue. This is where Vuex getters become handy. We implement our getter function ...