在 React 中,可以通过在项目根目录下创建一个名为 '.eslintrc.js' 的文件来配置 ESLint 规则。在这个文件中,你可以使用 'overrides' 属性来为特定的文件配置规则。

在这种情况下,你可以创建一个名为 '.eslintrc.js' 的文件,并将以下代码添加到文件中:

module.exports = {
  overrides: [
    {
      files: ['*.js'], // 匹配需要禁用规则的文件
      rules: {
        'no-undef': 'off', // 禁用 no-undef 规则
      },
    },
  ],
};

这样,ESLint 就会忽略所有 JavaScript 文件中的 'no-undef' 规则。请注意,这将会禁用该规则对所有文件的检查,包括 React 组件文件。

如果你只想禁用 'no-undef' 规则对特定文件的检查,可以将 'files' 属性中的 '*.js' 更改为特定的文件路径或通配符。例如,如果你只想禁用 'no-undef' 规则对 'src/utils/config.js' 文件的检查,可以将代码更改为:

module.exports = {
  overrides: [
    {
      files: ['src/utils/config.js'], // 匹配需要禁用规则的文件路径
      rules: {
        'no-undef': 'off', // 禁用 no-undef 规则
      },
    },
  ],
};

请确保在修改完 '.eslintrc.js' 文件后重新运行 ESLint 以使更改生效。

React 中解决 '// eslint-disable-next-line no-undef' 报错的最佳方法

原文地址: https://www.cveoy.top/t/topic/qC59 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录