vue.js 为什么我得到的是一个空数组而不是一个零数组?

dm7nw8vv  于 2023-03-31  发布在  Vue.js
关注(0)|答案(1)|浏览(177)

我有以下代码(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。当我打印salesTotalcogsTotal时,我得到的是[ 0, 0, 0, 0, 0 ],但是[ null, null, null, null, null ]得到的是grossMarginTotal。我做错了什么?

cczfrluj

cczfrluj1#

弄清楚了问题,正确代码如下:grossMarginAmount[k] = salesTotal.value[k] + cogsTotal.value[k];

相关问题