tsconfig.app.json 中的 baseUrl 未覆盖 tsconfig.json 中的基本 Url

baseUrl in tsconfig.app.json is not overiding the baseUrl in tsconfig.json

在 Angular 6 中,我使用 ng generate application 因为我将有多个应用程序必须共享一些库。通过这种方法,我的文件夹结构如下:

projects->app1->tsconfig.app.json

projects->app2->tsconfig.app.json

并且 tsconfig.jsonprojects 文件夹

处于同一级别

注意:在Angular中使用ng g application时会创建这样的文件夹结构 6.

我想在我的组件或服务等中使用相对路径进行导入,如下所示:

import { ExampleService } from 'src/services/example.service';

不必像这样使用它:

import { ExampleService } from '../../../services/example.service';

如何为如上所述创建的各个应用程序提供此功能?

我的tsconfig.json是这样的

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "downlevelIteration": true,
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

而我的tsconfig.app.json是这样的:

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "outDir": "../../out-tsc/app",
    "types": ["node"]
  },
  "exclude": ["test.ts", "**/*.spec.ts"]
}

我试过在 tsconfig.app.json 中这样给予:

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "../../out-tsc/app",
    "types": ["node"]
  },
  "exclude": ["test.ts", "**/*.spec.ts"]
}

但这没有用。

我想用相对路径直接用src来消除多级解析(../../../)

您应该看看 tsconfig.json 中的 paths compilerOption。有了它,您可以执行以下操作:

"compilerOptions": {
  "paths": {
    "@services/*": [
      "src/services/*"
    ]
  }
} 

然后您可以使用此路径从打字稿项目中的任何文件导入:

import { ExampleService } from '@services/example.service';

不用@,随便起什么名字