TypeScript 自定义路径在没有桶的情况下不起作用

TypeScript custom paths do not work without barreling

我花了整个上午 ts-paths 来缩短我的导入路径。

如果您设置桶装(索引文件导出您的模块),我可以确认自定义路径有效

root
├── client
│   ├──tsconfig.json
│   ├── src
│       ├──app
│           ├── shared
│               ├── services
│                   ├── some.service.ts
├── common
│   ├── index.ts // exports * from each file

我的 NG8 tsconfig.ts 文件:

"compilerOptions": {
    "baseUrl": ".",
    "paths": {
        "@common": ["../common"], // works with barreling
        "@services": ["./src/app/shared/services"] // only works with barreling
    },

@services 不起作用...除非我设置桶
(使用 export * from './some.service';shared 文件夹中添加 index.ts 文件)

@common 开箱即用,因为像上面那样滚动。

我是不是遗漏了什么或者我读过的所有内容都没有要求以这种方式导出您的模块?

我的研究:
https://angularfirebase.com/lessons/shorten-typescript-imports-in-an-angular-project/
https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping

还有很多...

根据您的设置(没有桶 index.ts 文件),您的路径需要略有不同。

"paths": {
  "@services/*": ["src/app/shared/services/*"],
},

然后您将能够使用以下方式导入:

import { SomeService } from '@services/some.service'