如何避免很长的路径并在 Create React App 项目中使用绝对路径?

How to avoid very long paths and use absolute paths within a Create React App project?

是否有 babel 插件来避免 CRA 中的长导入路径? 我在网上搜索了很多,但找不到实现此目标的最佳方法。

实际:

import MyComponent from '../../../../components/MyComponent'

预计

import MyComponent from 'components/MyComponent'

在你的主根中,create file jsconfig.json:

{
   "compilerOptions": {
      "baseUrl": "src"
   },
   "include": ["src"]
}

其中 src 是您存储项目文件的文件夹,有时可能是 /app/src

然后你就可以用绝对路径导入你的组件了:

import MyComponent from 'components/MyComponent';