[英]How to fix Eslint error “prefer-destructuring”?
我想像这样缩短 ES6 中的对象字面量:
const loc = this.props.local;
原因是loc.foo();
比this.props.local.foo();
更容易输入this.props.local.foo();
但现在 ESLint 抱怨:
使用对象解构:prefer-destructuring
我已经阅读了eslint.org 上的错误描述,但我不明白。 他们有一个看起来与我的代码非常相似的示例,但他们的示例似乎没问题?
var foo = object.bar;
如何修复错误而不将其设置为在.eslintrc
文件中忽略?
更改您的代码:
const local = this.props.local;
到:
const { local } = this.props;
它们是等效的,您可以以相同的方式调用local.foo()
。 除了第二次使用对象解构。
它是 ES 6 中的一个新构造,允许您在赋值中匹配对象的属性。 您需要的语法是:
const { local: loc } = this.props
转换为:“声明一个常量 loc 并从 this.props 为其分配本地属性的值”。
它告诉你使用
const {props: {local: loc}} = this;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.