如何使用vuex从命名空间模块访问getter?

时间:2019-08-19 20:22:34

标签: vue.js vuex

我的模块有:

export default {
    namespaced: true,
    state: {
        conversations: false
    },
    getters: {
        getConversation(state, ConversationId) {
            console.log('here i am!')
            let conversation = _.find(state.conversations, { id: ConversationId })
            console.log('conversation', conversation)
            return conversation
        },

在组件中,我正在尝试:

export default {
  name: "ConversationDetail",
  components: { HeaderSection },
  computed: {
    ...mapGetters("conversation", ["getConversation"]),
    ConversationId() {
      return this.$route.params.ConversationId;
    },
    conversation() {
      return this.getConversation(this.ConversationId);
    }
  },
  methods: {
    ...mapActions("conversation", ["loadConversation"])
  },
  mounted() {
    this.loadConversation(this.ConversationId);

但是出现错误:

Error in render: "TypeError: this.getConversation is not a function"

我在做什么错了?

1 个答案:

答案 0 :(得分:4)

您正确地引用了getter,但是,如果希望将参数传递给getter,则需要返回一个带有参数的函数,例如,使用咖喱lambda:

getter: (state) => (ConversationId) => {...}