无法解析tsconfig路径

时间:2018-08-22 08:50:47

标签: angular typescript angular6

我的TypeScript版本为“ 2.7.2”的Angular 6项目的tsconfig.json

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

我尝试使用上述路径导入服务

 import { AppService } from '@services/app.service';

但是我在运行ng serve时遇到此错误

  

src / app / app.module.ts(20,26)中的错误:错误TS2307:找不到模块'@ services / app.service'。

P.S。-没有路径

import { AppService } from './services/app.service';

工作正常。

1 个答案:

答案 0 :(得分:3)

paths选项是compilerOptions

的一部分
{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./src",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ],
    "paths":{
      "@services/*": ["app/services/*"] 
    }
  },
 }