javascript - 如何在反应中将我的导入缩短为“@dir/subdir/file”中的 import xxx
问题描述
我jsconfig.json
在我的根目录中将我的配置为:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@dir/*": ["./src/app/dir/*"]
}
},
"exclude": ["node_modules", "**/node_modules/*"],
}
我的文件夹结构如下所示:
root
│
└───src
│ |__app
| | |__dir
| | | |__subdir1
| | | | |__file1.js and other js files
| | | |__subdir2
| | | |__file2.js
|
|__jsconfig.json
|__package.json
|__...other root files
我的目标是使用将文件 1 中的某些内容导入文件 2
import xyz from '@dir/subdir1/file1'
而不是使用
import xyz from '../subdir1/file1'
解决方案
确保您的 webpack 配置webpack.config.js
最终具有:
对于弹出 webpack 配置后的 CRA,您应该能够导航到webpack.config.js
您的别名并将其添加到列表中。
resolve {
alias: {
"@dir": "src/app/dir"
},
}
或在 Next.js 中next.config.js
config.resolve.alias = {
...config.resolve.alias,
"@dir": "src/app/dir"
};
推荐阅读
- javascript - Does React.JS need to be installed locally to be able to view a web application that uses the React.JS library?
- python - Python 练习题 - 运行函数时出现名称错误
- ruby-on-rails - How to insert new line breaks using form_for with collection_check_boxes
- sql - 使用带有子查询的最大查询的 SQL/PL 行到列
- javascript - CSS 将顶部菜单项移动到汉堡菜单以实现更小的屏幕尺寸而不重复项
- php - 重构 PHP 代码以从本地函数中获取数据
- javascript - 带有位置的可滚动 iFrame:iOS 中的粘性元素
- cryptography - 当给定 2 个密文且公钥相差 1 位时,在 RSA 中查找原始消息
- office-js - 为什么我的换行标签没有解析?
- python - mysql.connector.cursor.execute() 静默进行,但尽管 commit() 不做任何更改