【问题标题】:vuex store classbased typescriptvuex 商店基于类的打字稿
【发布时间】:2020-05-05 02:03:49
【问题描述】:

大家好,我正在开发一个 Vue,Vuex 使用装饰器的强类型模板项目。

由于我是整个商店的新手,我无法弄清楚如何正确配置商店以在我的组件中按预期工作。

我最终想要实现的是调用我的不同商店模块的操作,如下所示:

this.$store.nameOfMyModule.NameOfAction
this.$store.<nameOfMyModule.NameOfGetter

一个更具体的项目示例是

this.$store.tickets.tickets
this.$store.tickets.fetchTickets

这里是repo的链接

【问题讨论】:

    标签: typescript vue.js vuex decorator store


    【解决方案1】:

    我认为最好使用mapActionsmapGetters

    import { mapActions, mapGetters } from 'vuex'
    
    //...
    
    computed: {
       ...mapGetters('tickets', [
            tickets
       ]),
       ...mapGetters('users', [
            users
       ])
    },
    
    methods : {
       ...mapActions('tickets', [
            fetchTickets
       ]),
       ...mapActions('users', [
            fetchUsers
       ])
    }
    //...
    

    之后,你可以直接在你的组件中使用this.fetchTickets()this.tickets

    最后你必须把fetchUsers放在action的users模块中,而不是fetchTickets

    【讨论】:

    • 所以 mapAction 的第一个字符串是指我的商店模块,第二个是我想要映射的所有函数的列表?
    • 我的 store.index 是否正确计算?我看到了多种不同的方式。另外,由于我没有方法或计算对象,我如何以类风格的 ts 格式使用扩展 mapActions 运算符。
    • 是的。您可以查看这些链接以了解如何制作自定义组件:github.com/vuejs/vue-class-componentstackoverflow.com/questions/51534273/…@Component({ methods: mapActions('ticket',[ Tickets]) })
    猜你喜欢
    • 2021-06-14
    • 2014-10-02
    • 2018-04-16
    • 2018-01-09
    • 2021-02-26
    • 1970-01-01
    • 2020-06-18
    • 1970-01-01
    • 2020-04-28
    相关资源
    最近更新 更多