基于vuex商店类的打字稿

问题描述 投票:0回答:1

嘿,我正在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个回答
0
投票

我认为最好使用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置于活动的users模块中,而不是fetchTickets

© www.soinside.com 2019 - 2024. All rights reserved.