简体   繁体   English

将参数传递给Vuex中的getter

[英]pass parameter to getters in Vuex

I have a Vue component like this: 我有一个像这样的Vue组件:

<script>
import { mapActions, mapGetters } from 'vuex'

export default {
  props: ['index'],
  computed: {
    ...mapGetters([
      'type',
      'width',
      'height',
      'description',
      'smtTagMeasureUnits',
      'tagValue'
    ])
  }
</script>

<template>
  <div :class='["block","module-"+type(index), "width"+width(index), "height"+height(index)]'>
    <h3>{{ description(index) }}</h3>
    <div class="data">
      <h1>{{ tagValue(index) }}</h1>
      <h2 class="measure">{{ smtTagMeasureUnits(index) }}</h2>
    </div>
  </div>
</template>

<style>
  ...
</style>

Parameter index , which comes into component as a prop , has been successfully passed to getters: 参数索引作为prop进入组件,已成功传递给getter:

getters: {
  ...
  type: (state, getters) => (par) => {
    return getters.widgetsConfig[par].type
  },
  width: (state, getters) => (par) => {
    if (getters.widgetsConfig[par].width) {
      return getters.widgetsConfig[par].width
    } return 2
  },
  height: (state, getters) => (par) => {
    if (getters.widgetsConfig[par].height) {
      return getters.widgetsConfig[par].height
    } return 1
  },
  ...
}

Works fine, but i'm not happy with this codestyle, because getterName(index) constantly repeats in the template part. 工作正常,但我对这种代码风格不满意,因为getterName(index)在模板部分不断重复。 All of my getters should carry index as a prop, so i'd like to have just getterName in the template and something like this in the script part: 我的所有getter都应该将索引作为prop,所以我想在模板中只使用getterName ,脚本部分就是这样的:

...mapGetters([
'type',
'width',
'height',
'description',
'smtTagMeasureUnits',
'tagValue'
], index)

Is it possible to achieve any codestyle improvements here? 是否有可能在这里实现任何代码改进?

You can always make computed properties that return the result of the getters. 您始终可以创建返回getter结果的计算属性。 Something like: 就像是:

export default {
  props: ['index'],
  computed: {
    ...mapGetters([
      'getTypeFromIndex',
      'getWidthFromIndex',
      'getHeightFromIndex'
    ]),
    height(): { return this.getHeightFromIndex(index) },
    width(): { return this.getWidthFromIndex(index) },
    type(): { return this.getTypeFromIndex(index) },

    //going a step further to clean up your templates...
    classList: [
        "block", 
        "height"+this.height,
        "width"+this.width,
    ]
  }

That way you just need height in your templates instead of height(index) , or even just classList if you take it that far 这样,你只需要height在你的模板,而不是height(index) ,甚至只是classList如果你把它远远

This is also referenced here: https://github.com/vuejs/vuex/issues/688 and I can't find it but I know I've seen that recommended by Evan You in a github issue as well. 这里也引用了这个: https//github.com/vuejs/vuex/issues/688我找不到它,但我知道我已经看过Evan You推荐的github问题了。

If you want to keep things DRY, it would make sense to leverage logic of getting item (entity that index corresponds to) information to the store, so component only receives full data that is ready to be rendered. 如果你想保持DRY,那么利用获取项目( index对应的实体)信息到存储的逻辑是有意义的,因此组件只接收准备好呈现的完整数据。

Suggested solution is to create a single getter, that accepts index as an argument and returns full list of options from getters.widgetsConfig . 建议的解决方案是创建一个getter,它接受index作为参数,并从getters.widgetsConfig返回完整的选项列表。

Note that if needed other getters may be re-used in order to collect necessary information into a single object. 请注意,如果需要,可以重新使用其他getter,以便将必要的信息收集到单个对象中。

Possible implementation: 可能的实施:

getters: {
  ...
  getItemByIndex: (state, getters) => (index) => {
    const { type, height, width } = getters.widgetsConfig[index]
    return {
      type,
      height,
      width
    }
  },
}

And update component to map a single getter and use it in computed property: 并更新组件以映射单个getter并在compute属性中使用它:

computed: {
  ...mapGetters([
    'getItemByIndex'
  ]),
  item () {
    return this.getItemByIndex(index)
  }
}

And all properties will be accessible inside a template via item.type , item.height , item.width , etc.. 并且可以通过item.typeitem.heightitem.width等在模板中访问所有属性。

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

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