我使用 create-react-app,我想使用来自 ./src
的绝对导入。
正如 Dan Abramov 推荐 的那样,我添加了 .env
和 NODE_PATH=src
并且它有效。
但是我的 eslint 不明白该模块已经存在。我得到错误 import/no-unresolved
和 import/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 许可协议
您需要使用
eslint-plugin-import
: https ://github.com/benmosher/eslint-plugin-import并在—中配置你的eslint设置
.eslintrc
然后,您可以使用从
src
文件夹导入。例如,如果你有
src/components/MyComponent.jsx
,你将这样写: