How to use getters in Vuex - Educative.io

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

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.



請為這篇文章評分?