【问题标题】:Vuex Better way to Map?Vuex 更好的映射方式?
【发布时间】:2020-01-11 10:05:16
【问题描述】:

我刚刚开始学习 Vuex,目前正在使用模块。 mapStatemapMutationsmapActionsmapGetters 还有比这更好的方法吗?

    ...mapActions('FootageModal', ['setElements', 'setActiveFootage']),
    ...mapActions('Scene', ['addScene', 'deleteScene']),
    ...mapMutations('Content', ['resetActiveContent']),
    ...mapGetters({
      getSceneStatus: 'Scene/getStatus',
      getStatus: 'getStatus'
    }),

【问题讨论】:

    标签: javascript vue.js vuejs2 vuex vuex-modules


    【解决方案1】:

    您使用mapGetters 的方式很好。您可以对其余部分执行相同操作:

    ...mapActions({
          someAction: 'someAction',
          footageModal: 'FootageModal'
    })
    
    ...mapMutations({
        someMutation: 'SET_SOME_MUTATION'
    }),
    

    同样值得注意。您应该添加 mapGettersmapState 作为计算属性,并添加 mapActionsmapMutations 作为方法。

    【讨论】:

    • 您的 mapActions 示例与 OP 的意图不符。 OP 正在映射来自FootageModal 模块的两个操作。您映射了一个名为“FootageModal”的动作。这些是不等价的。
    • 您好,感谢您的评论。 ...mapAction({ footageModal: 'FootageModal'}),这会在 FootageModal 模块中获得所有 Actions 吗?
    • @user3779015,不,...mapAction({ footageModal: 'FootageModal'}) 将在根命名空间中查找名为“FootageModal”的操作。如果没有找到,它将失败。
    猜你喜欢
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    • 2021-08-25
    • 2014-04-28
    • 1970-01-01
    • 2011-12-18
    相关资源
    最近更新 更多