【问题标题】:Where to put parameterised mapGetters (in computed or methods of component)将参数化的 mapGetters 放在哪里(在组件的计算或方法中)
【发布时间】:2020-09-26 11:58:42
【问题描述】:

在 vuex 中,我有这个:

getByLessonId: state => _lessonId => {
  return state.entities.filter(e => e.lesson.id === _lessonId);
},

在组件中:

// using as a method...
...mapGetters("assignment", { getAssignmentsByLessonId: "getByLessonId" }),

后面的代码,既然映射返回的是函数,那我需要这样调用吗?

// load this lessons assignments...
this.assignments = this.getAssignmentsByLessonId()(this.id);

// this is what i started with
// this.$store.getters["assignment/getByLessonId"](this.id)

它有效,只是不确定是否有更好的方法来做到这一点?还是应该将mapGetters 放在组件的计算属性中?

【问题讨论】:

    标签: vue.js vuejs2 vue-component vuex


    【解决方案1】:

    最好将 mapGetters(只要 mapState)放在组件的 computed 部分。这是使用状态的多个 getter 或 props 的推荐方法,因为您只有一个地方来控制组件中的 getter 和 props。

    【讨论】:

    • 为什么将 mapgetter 放在组件方法中而不是计算方法中是一件坏事?我还想将参数传递给我的 getter。
    • 我的意思是反对直接的 getter 调用或访问状态道具。如果将getters 放在methods 部分中,您已经提到您必须先使用附加的() 才能获得真正的getter(与调用通过computed 声明的这些getter 相比)。
    猜你喜欢
    • 2019-11-16
    • 2013-09-22
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    相关资源
    最近更新 更多