简体   繁体   English

将 prop 值传递给组件内的计算属性

[英]pass prop value to computed properties inside a component

Is it possible/good practice to use a prop value inside a computed property function inside a component?在组件内的计算属性函数中使用 prop 值是否可能/好的做法? If so, how do I build the return with this prop?如果是这样,我如何用这个道具建立回报?

Carousel.vue Carousel.vue

props: [
  'source',
],
computed: {
   items () {
     return this.$store.state.(prop value source here).list
   }
}

store/categorya.js (the same for categoryb and categoryc) store/categorya.js (categoryb 和 categoryc 相同)

import categorya from '(...)'
export const state = () => ({
  list: categorya
})

Update更新

Index.vue索引.vue

carousel(source="categorya")
carousel(source="categoryb")
carousel(source="categoryc")

The question is a bit unclear.问题有点不清楚。 If source specifies a Vuex module name you could use bracket notation:如果 source 指定了 Vuex 模块名称,您可以使用括号表示法:

computed: {
  items () {
    return this.$store.state[this.source].list
  }
}

AFTER YOUR EDIT编辑后

Still unclear, but if there are no modules and list is a property of root state, then you would simply use:仍然不清楚,但是如果没有模块并且list是根状态的属性,那么您只需使用:

computed: {
  items () {
    return this.$store.state.list
  }
}

The fact that list is first defined by an import called categorya makes no difference. list首先由名为categorya的导入定义的事实没有区别。 It's still the only state available and its name is just list .它仍然是唯一可用的状态,它的名字只是list

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM