如何更改 eslint 设置以了解绝对导入?

新手上路,请多包涵

我使用 create-react-app,我想使用来自 ./src 的绝对导入。

正如 Dan Abramov 推荐 的那样,我添加了 .envNODE_PATH=src 并且它有效。

但是我的 eslint 不明白该模块已经存在。我得到错误 import/no-unresolvedimport/extensions

这是我的 eslint 配置:

 module.exports = {
parser: 'babel-eslint',
extends: 'airbnb',
rules: {
    'react/no-did-mount-set-state': 'off',
    'import/no-extraneous-dependencies': 'off',
    'no-use-before-define': 'off',
    'arrow-body-style': 'off',
    // uncommit on developing
    'no-console': 'off',
    'no-debugger': 'off',
  },
  globals: {
    browser: true,
    fetch: true,
    serviceworker: true,
    describe: true,
    it: true,
    expect: true,
    document: true,
  },

};

当然,如果 vscode 会通过“src”为我提出建议,那就太好了。

原文由 Alexander Knyazev 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 268
1 个回答

您需要使用 eslint-plugin-importhttps ://github.com/benmosher/eslint-plugin-import

并在—中配置你的eslint设置 .eslintrc

 module.exports = {
  ...
  "settings": {
    "import/resolver": {
      "node": {
        "paths": ["src"]
      }
    },
  },
}

然后,您可以使用从 src 文件夹导入。

例如,如果你有 src/components/MyComponent.jsx ,你将这样写:

 import MyComponent from 'components/MyComponent.jsx';

原文由 Guillaume Jasmin 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题