0

要检索 Vue 3 组件中的对象列表,我在 Vuex 4 中使用了一个 getter,如下所示:

    getAccountingEntries: (state) => {
    let balance = 0;
    return state.entries.filter((entry) => {
      balance = balance + (entry.revenue - entry.expenditure);
      entry.balance = balance;
      return (
        entry.type === 'gj' &&
        entry.year === state.yearDisplayed &&
        entry.userID === state.currentUserID
      );
    });
  }

要检索 Vue 3 组件中的对象列表,我在 Vuex 4 中使用了一个 getter,如下所示:

您会注意到我执行了一个计算并将结果添加到一个名为 balance 的新属性中。

它完美地工作。

但我注意到在对象上添加 balance 属性也反映在 state.entries 中的对象中。

我不喜欢我的 getAccountingEntries 函数在不经历突变的情况下更改状态,尽管它似乎工作正常。

我做错了吗?有没有办法避免这种副作用?

谢谢您的帮助。

4

0 回答 0