如何修复Eslint错误“prefer-destructuring”? [英] How to fix Eslint error "prefer-destructuring"?
问题描述
我想在ES6中缩短对象文字,如下所示:
I wanted to shorten an object literal in ES6 like this:
const loc = this.props.local;
原因是 loc.foo();
比更容易输入this.props.local.foo();
但现在ESLint抱怨:
But now ESLint complains:
使用对象解构:prefer-destructuring
Use object destructuring: prefer-destructuring
我在eslint.org上阅读了错误说明,但我不知道明白它。他们有一个看起来与我的代码非常相似的例子,但他们似乎没问题?
I've read the error description on eslint.org but I don't understand it. They have an example which looks very similar to my code but theirs seem to be ok?
var foo = object.bar;
如何修复错误而不将其设置为忽略 .eslintrc
file?
How can I fix the error without setting it to ignore in the .eslintrc
file?
推荐答案
更改您的代码:
const local = this.props.local;
to:
const { local } = this.props;
它们是等价的,您可以调用 local.foo()
以同样的方式。除了第二次使用对象解构。
They are equivalent and you can call local.foo()
in the same way. except that the second use object destructuring.
这篇关于如何修复Eslint错误“prefer-destructuring”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!