将参数传递给映射的吸气剂时,为什么会得到未定义的信息?

时间:2018-10-17 15:02:00

标签: vue.js vuejs2 vuex

我设置了一些吸气剂,将其传递给他们一个ID,然后它们返回相关事件数据。所以我已经将它们映射到一个组件中,但是当传递参数时,该参数是不确定的。

组件:

<template>
    <div>
        <h1>{{ category.name }}</h1>
    </div>
</template>

<script>
    import { mapGetters } from 'vuex'

    export default {
        props: ['id'],

        computed: mapGetters({
            subCategories: ['categories/getSubcategories'](this.id),
            category: ['categories/getCategory'](this.id)
        })
    }
</script>

字母:

getCategory: (state, id) => {
    return state.categories.filter(category => category.id === id);
},

错误为:

  

无法读取未定义的属性“ id”

如果我将其传递为硬编码参数,例如category: ['categories/getCategory'](106),则会得到:

  

['categories / getCategory']不是函数

我在哪里错了?

编辑:这是我的商店:console.log(this.$store)

1 个答案:

答案 0 :(得分:1)

根据this GitHub issue,看来您需要在getter中返回一个函数,然后在计算属性中调用该方法,即您的getter:

getCategory: state => {
  return id => state.categories.filter(category => category.id === id);
}

然后在computed属性中:

computed: {
  ...mapGetters([
    'categories/getSubcategories', 
    'categories/getCategory'
  ]),
  subCategories () {
    return this['categories/getSubcategories'](this.id)
  },
  category () {
    return this['categories/getCategory'](this.id)
  }
}