[Vuejs]-How to compute a getter in vue?

0๐Ÿ‘

โœ…

You should handle the else case :

computed: {
    features() {
      if(this.$store.getters.features) {       
        return this.$store.getters.features   
      } else{
       return false;
      }
    }
  

but itโ€™s recommended to use the second syntax.

0๐Ÿ‘

In a computed property, you should always ensure that a value is returned, no matter what condition is met.

In your computed property, you have an if statement that checks if this.$store.getters.features is truthy, and if it is, it returns the value. However, if the condition is not met (if this.$store.getters.features is falsy), the function does not return anything, which is the cause of the error.

Example :

computed: {
  features() {
    if (this.$store.getters.features) {
      return this.$store.getters.features;
    } else {
      return null; // You can use any return value here as per requirement
    }
  }
}

Leave a comment