【问题标题】:Unexpected side effect in "currentData" computed property“currentData”计算属性中的意外副作用
【发布时间】:2021-02-08 00:29:42
【问题描述】:

如何在 Vue.js 中从 computed 返回一个数组?

我收到此错误:

错误“currentData”计算属性 vue/no-side-effects-in-computed-properties 中的意外副作用...

我的代码如下:

export default {
  computed: {
    currentData: function() {
      this.series[0].data = [1, 2, 3, 4, 5, 6, 7, 8];
      return this.series[0].data;
    }
  }
};

<template>
  <div>
    {{currentData}}
  </div>
</template>

【问题讨论】:

  • 您将 this.series[0].data 的值更改为 [1,2,3,4,5,6]...您无法在计算中执行此操作...您只能根据您的状态返回数据。这就是为什么

标签: javascript vue.js


【解决方案1】:

我不是 Vue 专家,但是你得到的错误很清楚 - 你不应该在计算属性中改变你的状态。您只能参考它们并生成一些只读响应作为结果。请查看this link。请删除

this.series[0].data=[1,2,3,4,5,6,7,8];

并在其他地方更新它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2019-10-03
    • 2020-09-20
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多