【发布时间】:2023-01-17 06:11:00
【问题描述】:
我刚刚启动了 CRA npx create-react-app my-app --template typescript,我想在调用组件时创建一个别名,例如:
import components from '@components'
其中组件位于src/components。
我尝试通过添加以下内容在tsconfig.json 中进行配置:
{
"compilerOptions": {
...
"baseUrl": "./src",
"paths": {
"@utils/": ["./utils/"],
"@utils/*": ["./utils/*"]
}
}
}
同样在webpack.config.js中添加:
// const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin')
const path = require('path')
module.exports = {
resolve: {
// plugins: [new TsconfigPathsPlugin()],
alias: {
'@utils': path.resolve(__dirname, './src/utils/'),
'@utils/*': path.resolve(__dirname, './src/utils/*')
}
}
}
但它仍然不起作用。
任何人都可以帮我解决这些问题吗?但是,我不会使用其他库,例如@craco/craco。
【问题讨论】:
标签: reactjs webpack create-react-app react-typescript