javascript - 如何修复 Eslint 错误 "prefer-destructuring"?

标签 javascript ecmascript-6 eslint

我想像这样缩短 ES6 中的对象文字:

const loc = this.props.local;

原因是 loc.foo();this.props.local.foo(); 更容易输入

但是现在 ESLint 提示:

Use object destructuring: prefer-destructuring

我读过 error description on eslint.org但我不明白。他们有一个看起来与我的代码非常相似的示例,但他们的代码似乎没问题?

var foo = object.bar;

如何修复错误而不在 .eslintrc 文件中将其设置为忽略?

最佳答案

更改您的代码:

const local = this.props.local;

到:

const { local } = this.props;

它们是等价的,你可以用同样的方式调用local.foo()。除了第二次使用对象解构。

关于javascript - 如何修复 Eslint 错误 "prefer-destructuring"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47395070/

相关文章:

javascript - 在 Nodejs 中导出对象时,值未定义

javascript - ReactJs 换时间视频html5

javascript - 在 es6 中,我需要 promise 在完成后返回 true 或 false

javascript - “修复所有可自动修复的问题”并没有修复尽可能多的问题 vscode-eslint

visual-studio-code - ESLint 没有运行,因为不推荐使用的设置 'eslint.enable' 设置为 false。删除设置并使用扩展禁用功能

javascript - 如何设置 eslint 以仅在单文件 vue 组件中将 lodash 检测为全局?

javascript - jQuery,如果 "a"标签是否有类

javascript - JavaScript 中的手动悬停功能

javascript - 在 Heroku 上运行 ES6 应用程序

ecmascript-6 - 为什么 ES6 使用 $ AND 大括号来表示模板文字?