0

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?

4
  • 1
    Why not track the current kind of person in a prop in your component? Commented Apr 24, 2018 at 19:00
  • @gnud because I want to make some changes within computed before I will send it as a prop Commented Apr 24, 2018 at 19:01
  • 2
    Sure - but some part of your app knows the current person. If you somehow share that data with the component that has the computed property, your problem is solved. Where does offer.person come from/live? You're omitting some crucial part of your code here Commented Apr 24, 2018 at 19:04
  • offer.person is from data() {return { inside my component. I used this.$store.state.${offer.person}ListSpeciality inside props before and this worked fine Commented Apr 24, 2018 at 19:10

1 Answer 1

1

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']
     }
   }
Sign up to request clarification or add additional context in comments.

3 Comments

This might me an opiton, but I would have to code it for every case of offer.person possible value - and I don't want to do it
If the naming is always the way you typed it here, you can do this.$store.state[this.offer.person + 'ListSpeciality']. Might be smart to check if this key actually exists in the store, though.
if the @gnud solution works then great solution @gnud!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.