如何避免在Create React App上走很长的路?

问题描述 投票:4回答:1

是否有babel插件来避免CRA中的长导入路径?我在网上进行了大量搜索,但找不到最佳的方法。

实际:

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

预期

import MyComponent from 'components/MyComponent'
javascript reactjs babeljs babel create-react-app
1个回答
7
投票

在您的主根目录中,create file jsconfig.json

jsconfig.json

{ "compilerOptions": { "baseUrl": "src" }, "include": ["src"] } 是存储项目文件的文件夹,有时它可能是src/app

然后您将能够使用绝对路径导入组件:

/src
© www.soinside.com 2019 - 2024. All rights reserved.