【问题标题】:How to configure paths property in tsconfig.json file for external libraries如何在 tsconfig.json 文件中为外部库配置路径属性
【发布时间】:2019-11-16 05:08:02
【问题描述】:

在这里 [a link] 找到了我正在尝试做的最接近的解释。我仍然在努力编译我的解决方案。

我有我想在我的项目中使用的外部类型,并且它们都具有命名空间“DS/”的 .d.ts 文件,我将这些文件存储在一个名为 typings 的文件夹中。下面是我的简单 Angular 应用程序的文件夹结构。

-MyAppName
--typings
----all my modules d.ts
--src
----app
-tsconfig.json

这是我的 tsconfig.json 文件,如下所示

"compileOnSave": false,
  "compilerOptions": {
    "baseUrl": ".",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ],
    "paths": {
      "DS/*": [
        "typings/*"
      ],
      "*": [
        "src/*"
      ]
    }
  },
  "include": [
    "typings/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }

导入库时出现错误,如下所示 从 'DS/MyModuleName' 导入 { MyModuleName };

./src/app/app.module.ts 中的错误 未找到模块:错误:无法解析“MyAppName\src\app”中的“DS/MyModuleName”

我正在使用 AngularCLI 来构建应用程序。我未能配置我的 tsconfig 文件以支持这些外部导入的库。不知道我做错了什么。请帮忙。

【问题讨论】:

    标签: angular typescript typescript-typings typescript2.0


    【解决方案1】:

    根据您的树,在您的 tsconfig.ts 中尝试

    "typeRoots": ["node_modules/@types", "typings" ]...
    
    "paths": {
      "DS/*": [
        "typings/*"
      ],
    }
    
    "include": ["src", "typings"],
    

    然后在你的 app.module.ts 中

    import { MyModuleName} from 'DS/myModuleName.module';
    

    【讨论】:

    • 嗨,谢谢你的回复,但我仍然收到同样的错误ERROR in ./src/app/app.module.ts Module not found: Error: Can't resolve '../../typings/MyModuleName' in 'MyAppName\src\app'
    • 我还认为设置"paths": { "DS/*": [ "typings/*" ], 的全部目的是为了将导入称为“DS/...”?
    • 在我的应用程序中对其进行了模拟,并发现了一个有效的组合。请参阅上面的编辑答案并告诉我。欢呼
    • 我在这些库的可视代码编辑器中没有收到任何错误。自动完成可以正确识别库,但是当我执行 ng build 时,它仍然会抛出错误 ERROR in ./src/app/app.module.ts Module not found: Error: Can't resolve 'DS/MyModuleName/shared.module' in 'MyAppName\src\app'
    猜你喜欢
    • 1970-01-01
    • 2019-11-09
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 2021-11-02
    • 2016-04-03
    相关资源
    最近更新 更多