简体   繁体   English

如何在tsconfig.json文件中为外部库配置路径属性

[英]How to configure paths property in tsconfig.json file for external libraries

The closest explanation for what I am trying to do was found here [a link] . [链接]中找到了我想要做的最接近的解释。 Still I am struggling to get my solution to compile. 不过,我仍在努力寻求解决方案来进行编译。

I have external typings which I would like to use in my project and all of them has .d.ts files with namespace "DS/ " which I store in a folder called typings. 我有我要在项目中使用的外部类型,并且所有类型都有.d.ts文件,其名称空间为“ DS / ”,我将其存储在名为“类型”的文件夹中。 Below is my folder structure for my simple Angular app. 下面是我简单的Angular应用程序的文件夹结构。

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

this is my tsconfig.json file look like below 这是我的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 } 

I get an error for importing my library as below import { MyModuleName } from 'DS/MyModuleName'; 从“ DS / MyModuleName”导入{MyModuleName}时,导入库出现错误。

ERROR in ./src/app/app.module.ts Module not found: Error: Can't resolve 'DS/MyModuleName' in 'MyAppName\\src\\app' ./src/app/app.module.ts中的错误找不到模块:错误:无法解析“ MyAppName \\ src \\ app”中的“ DS / MyModuleName”

I'm using AngularCLI to build the app. 我正在使用AngularCLI来构建应用程序。 I have failed to configure my tsconfig file to support these externally imported libraries. 我无法将tsconfig文件配置为支持这些外部导入的库。 Not sure what I am doing wrong. 不知道我在做什么错。 Please help. 请帮忙。

according to your tree, try in your tsconfig.ts 根据您的树,尝试在您的tsconfig.ts中

"typeRoots": ["node_modules/@types", "typings" ]...

"paths": {
  "DS/*": [
    "typings/*"
  ],
}

"include": ["src", "typings"],

then in your app.module.ts 然后在你的app.module.ts中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM