首页 > 解决方案 > eslint - 在我的 vue 项目中使用数组解构

问题描述

我有这样一条线this.report = newVal[0];。我正在查看文档,我很困惑。

我见过的例子就像

const local = this.propslocal
const {local} = this.props

有任何想法吗?

标签: vue.jseslint

解决方案


看看这个:eslint prefer-destructuring rule

此规则强制使用解构而不是通过成员表达式访问属性。

正如您所看到的示例,您应该更改:

this.report = newVal[0]

为此:

[this.report] = newVal;

为了让您清楚,如果newVal有 4 个项目,并且您想将前 2 个项目存储到不同的变量中,而不是这样做:

const a = newVal[0];
const b = newVal[1];

使用解构,你应该这样做:

const [a, b] = newVal;

推荐阅读