简体   繁体   English

如何在 Vuex 模块(vueJs)中过滤命名空间的 getter

[英]how to filter namespaced getter in Vuex module (vueJs)

My Getter in module called ShopItemCategory我在名为 ShopItemCategory 的模块中的 Getter

    getters: {
        shopItemsCategories: state => state.ShopItemsCategories.data,
    },

And this is my computed function inside component这是我在组件内部计算的 function

  computed: {
    shopItemsCategories() {
      return this.$store.getters['ShopItemCategory/shopItemsCategories'].filter(c => c.shop_id == this.$route.params.id)
    },
    },

when I click save the filter working well but after refreshing it gives error当我点击保存过滤器运行良好但刷新后它给出错误

Vue warn]: Error in render: "TypeError: Cannot read property 'filter' of undefined"

and this is the mutation that dispatched in mounted这是在mounted中发送的突变

    mutations: {
        SET_SHOP_ITEM_CATEGORIES(state, shopItemsCategories) {
            state.ShopItemsCategories = shopItemsCategories;
        },
        }

and this is the dispatch command inside mounted() in the component这是组件中mounted()内的调度命令

  mounted() {
    this.$store.dispatch('ShopItemCategory/getShopItemsCategories');
  },

Until your SET_SHOP_ITEM_CATEGORIES mutation is executed, your getter will use whatever initial data you have in your module's state .在执行您的SET_SHOP_ITEM_CATEGORIES突变之前,您的 getter 将使用您在模块的state中拥有的任何初始数据。 If that ShopItemsCategories.data property is not defined, you will get the reported error message when your computed property executes.如果ShopItemsCategories.data属性,您将在计算属性执行时收到报告的错误消息。

The simple solution is to define some initial state that makes sense for the rest of your app.简单的解决方案是定义一些对应用程序的 rest 有意义的初始 state。

For example例如

state: {
  ShopItemsCategories: {
    data: []
  }
}

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

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