给路径设置别名, 借助于 craco
程序员文章站
2022-03-01 17:21:26
...
React 中给路径设置别名 借助 craco
实现。
craco.config.json
const path = require("path");
const resolve = dir => path.resolve(__dirname, dir);
module.exports = {
.....
webpack: {
alias: {
"@": resolve("src"),
"components": resolve("src/components")
}
}
}
导入
import Header from '@/components/Header';
import Header from 'components/Header';