Home > front end >  Vue: how to use a mapState in a computed property?
Vue: how to use a mapState in a computed property?

Time:12-12

I am trying to use mapState and have that state then be using a computed property right beneath it.

 computed: {
    ...mapState(['features']),

    shouldShowExtras(): boolean {
      if (this.features.hasVIP) {
        return true;
      }
      return false;
    },
  }

I can't pinpoint the issue but all of my tests now come back with TypeError: Cannot read properties of undefined (reading 'state') .

Before I go through test by test, does the usage seem correct as far as using the mapped state in another computed property?

CodePudding user response:

Your syntax should be fine, based on this article I found. (It helps a lot to understand what exactly is going on.)

I would guess, that you can find "state.properties"/"this.$state.properties" in your application where the state is "undefined", therefore the properties variable cannot be evaluated/found.

CodePudding user response:

Why you don't create getters at vuex and call getter instead of state?

getters:{
 features: (state) => state.features
}

 computed: {
    ...mapGtters(['features']),
}
  • Related