我在解决项目中的 TypeScript 路径别名时遇到问题。我已使用“baseUrl”和“paths”设置将 tsconfig.json 文件配置为包含路径别名,但别名导入未按预期工作。
我想将其作为别名导入导入,如下所示: 从“@/src/components/ui/button”导入{ThemeButton};
它不适用于别名导入,但当我像这样导入它时它可以工作。 从“../../ui/button”导入 { ThemeButton };
我已更新我的 tsconfig.json 以包含此内容,但它不起作用
"baseUrl": "./",
"paths": {
"@/*": ["./src/*"]
}
这是我完整的 tsconfig.json 文件:
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "commonjs",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
这会很好用:
import { ThemeButton } from "@/components/ui/button";