vue.jsif-statementvuejs2gettercomputed-properties

How to compute a getter in vue?


I'm expecting to display a computed getter property, but VSC gives me the error "Expected to return a value in 'features' computed property." Even though I'm returnng a true value in getter in code.

// Doesn't work
computed: {
    features() {
      if(this.$store.getters.features) {        // feature is definately true by itsels (returns true)
        return this.$store.getters.features   // error comes here
      }
    }
  }
  
  // Works fine
computed: {
    features() {
        return this.$store.getters.features 
      }
    }
  }


Solution

  • 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.