如何使用“@”符号导入模块,Node+Express+Typescript

raj*_*aju 4 node.js express typescript

我对 Node+Typescript 比较陌生,我有一个使用这样导入另一个文件模块的文件import { IS_PRODUCTION } from '@/config/config';,我无法理解它是如何通过@符号导入的。

需要一些帮助来理解这一点。

谢谢

[编辑]:这是 tsconfig.json

{
  "compilerOptions": {
    "esModuleInterop": true,
    "target": "es2018",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "sourceMap": true,
    "outDir": "build",
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"],
      "@@/*": ["./*"]
    },
    "module": "commonjs",
    "skipLibCheck": true
  },
  "include": ["src/**/*"],
  "exclude": ["build", "node_modules"]
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*yne 10

tsconfig.json有一些自定义路径设置。其中之一是这样的:

"@/*": ["./src/*"],
Run Code Online (Sandbox Code Playgroud)

这意味着当打字稿看到以 then 开头的路径时,@/whatever它会将其转换为path/of/tsconfig/here/src/whatever.

这很好,因为它允许您使用项目根目录的路径加载文件。这样,如果您移动文件,则不必更改所有导入。

您可以在文档中阅读有关此功能的信息