在 VueJS 组件中,将 vuex getter 映射到不同的名称不起作用
In VueJS component mapping vuex getters to a different name not working
computed: {
...mapGetters('global/train_ticket', [
{ ticketTypeForTrain: 'getTicketType' },
{ ticketFareForTrain: 'getTicketFare' },
]),
},
mounted() {
console.log(this.ticketTypeForTrain + ' ' + this.ticketFareForTrain)
},
在上面的代码中,我已经明确命名了 getter。当我试图在 mounted
钩子中获取它们时,我得到 undefined undefined
如果我不明确命名它们,我可以获取如下值
computed: {
...mapGetters('global/train_ticket', [
'getTicketType', 'getTicketFare'
]),
},
mounted() {
console.log(this.getTicketType+ ' ' + this.getTicketFare)
}
为什么命名 getter 不起作用?
谢谢
如 docs 中所示:
如果要将 getter 映射到其他名称,请使用对象
因此您的代码应该使用对象而不是数组,如下所示:
computed: {
...mapGetters('global/train_ticket', {
ticketTypeForTrain: 'getTicketType',
ticketFareForTrain: 'getTicketFare',
}),
},
mounted() {
console.log(this.ticketTypeForTrain + ' ' + this.ticketFareForTrain)
},
computed: {
...mapGetters('global/train_ticket', [
{ ticketTypeForTrain: 'getTicketType' },
{ ticketFareForTrain: 'getTicketFare' },
]),
},
mounted() {
console.log(this.ticketTypeForTrain + ' ' + this.ticketFareForTrain)
},
在上面的代码中,我已经明确命名了 getter。当我试图在 mounted
钩子中获取它们时,我得到 undefined undefined
如果我不明确命名它们,我可以获取如下值
computed: {
...mapGetters('global/train_ticket', [
'getTicketType', 'getTicketFare'
]),
},
mounted() {
console.log(this.getTicketType+ ' ' + this.getTicketFare)
}
为什么命名 getter 不起作用?
谢谢
如 docs 中所示:
如果要将 getter 映射到其他名称,请使用对象
因此您的代码应该使用对象而不是数组,如下所示:
computed: {
...mapGetters('global/train_ticket', {
ticketTypeForTrain: 'getTicketType',
ticketFareForTrain: 'getTicketFare',
}),
},
mounted() {
console.log(this.ticketTypeForTrain + ' ' + this.ticketFareForTrain)
},