【问题标题】:Cannot resolve tsconfig paths无法解析 tsconfig 路径
【发布时间】:2019-01-28 11:49:47
【问题描述】:

我的 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';

工作正常。

【问题讨论】:

    标签: angular typescript angular6


    【解决方案1】:

    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/*"] 
        }
      },
     }
    

    【讨论】:

    • 就是这么简单。我觉得很愚蠢,错过了这一点。非常感谢朋友。
    猜你喜欢
    • 2019-04-09
    • 2020-05-06
    • 2021-12-08
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多