gileneusz
gileneusz

Reputation: 1495

vuejs - changing return value inside computed

I use simply computed to get correct values from my array to select field:

computed: {
   specialitySelect() {
   return this.$store.state.physicianListSpeciality
   }
}

this works fine, but I need to get proper values depend on offer.person value, which could be: physician, nurse etc...

How can I change return value to be: in case of nurse:

computed: {
   specialitySelect() {
   return this.$store.state.nurseListSpeciality
   }
}

If this would be a function I could easily do:

methods: {
   specialitySelect(person) {
   return this.$store.state[person]ListSpeciality
   }
}

but in computed I cannot do it. Is there any other solution that I could use instead?

Upvotes: 0

Views: 1478

Answers (1)

Roland
Roland

Reputation: 27819

One solution would be to check the value of offer.person and depend on that return what you want to return:

 computed: {
   specialitySelect() {
    switch(this.offer.person) {
       case 'nurse':
         return this.$store.state.nurseListSpeciality
         break
       case 'physician':
         return this.$store.state.physicianListSpeciality
         break
       default:
         return []
    }
    return this.$store.state.nurseListSpeciality
   }
 }

Note: Thanks to a comment in this answer a great solution would be:

   computed:{
     specialitySelect() {
        return this.$store.state[this.offer.person + 'ListSpeciality']
     }
   }

Upvotes: 1

Related Questions