Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vue.js vuejs-更改计算内部的返回值_Vue.js - Fatal编程技术网

Vue.js vuejs-更改计算内部的返回值

Vue.js vuejs-更改计算内部的返回值,vue.js,Vue.js,我使用simply computed从数组中获取正确的值以选择字段: 这很好,但我需要得到适当的价值取决于提供。人的价值,可以是:医生,护士等 如何将返回值更改为:如果是护士: computed: { specialitySelect() { return this.$store.state.nurseListSpeciality } } 如果这是一个我可以轻松实现的功能: methods: { specialitySelect(person) { return

我使用simply computed从数组中获取正确的值以选择字段:

这很好,但我需要得到适当的价值取决于提供。人的价值,可以是:医生,护士等

如何将返回值更改为:如果是护士:

computed: {
   specialitySelect() {
   return this.$store.state.nurseListSpeciality
   }
}
如果这是一个我可以轻松实现的功能:

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

但实际上我做不到。是否有其他解决方案可供我使用?

一种解决方案是检查offer.person的值,并根据您想要返回的内容返回:

 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
   }
 }
注意:由于本回答中的评论,一个很好的解决方案是:

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

为什么不在组件中的道具中跟踪当前类型的人?@gnud,因为我想在将其作为道具发送之前在computed中进行一些更改-但是您的应用程序的某些部分知道当前的人。如果您以某种方式与具有计算属性的组件共享该数据,您的问题就解决了。offer.person来自/住在哪里?您忽略了代码中的一些关键部分。offer.person来自我的组件中的数据{return{。我使用了它。$store.state。${offer.person}ListSpeciality以前在道具中使用过,效果很好这可能是我的看法,但我必须为每种情况下的offer.person可能值进行编码-我不想这样做如果命名始终是您在此处键入的方式,您可以这样做。$store.state[this.offer.person+'ListSpeciality']。不过,检查此密钥是否确实存在于商店中可能是明智的。如果@gnud解决方案有效,那么伟大的解决方案@gnud!
   computed:{
     specialitySelect() {
        return this.$store.state[this.offer.person + 'ListSpeciality']
     }
   }