我有以下代码(vue 3):
const salesTotal = computed(() => {
let totalSalesAmount = new Array(numberOfPeriods).fill(0);
return totalSalesAmount;
});
const cogsTotal = computed(() => {
let totalCOGSAmount = new Array(numberOfPeriods).fill(0);
return totalCOGSAmount;
});
const grossMarginTotal = computed(() => {
let grossMarginAmount = [];
for (let k = 0; k < numberOfPeriods; k++) {
grossMarginAmount[k] = salesTotal[k] + cogsTotal[k];
}
return grossMarginAmount;
});
numberOfPeriods是5。当我打印salesTotal
和cogsTotal
时,我得到的是[ 0, 0, 0, 0, 0 ]
,但是[ null, null, null, null, null ]
得到的是grossMarginTotal
。我做错了什么?
1条答案
按热度按时间cczfrluj1#
弄清楚了问题,正确代码如下:
grossMarginAmount[k] = salesTotal.value[k] + cogsTotal.value[k];