关于 Angular 项目类型为 library 的工程使用 tsconfig.json 的问题
我今天做 Angular 開發時,遇到一個很奇怪的問題:
在某個 Angular library 項目里,無法正確解析出 @Spartacus/core,
錯誤消息:
Cannot find module ‘@spartacus/core’ or its corresponding type declarations.ts(2307)
但是,就在同一工程的單元測試 .spec.ts 文件里,庫的解析是正確的。這種在同一文件夾里不同文件對另外同一個庫的解析,出現如此不一致的現象,讓我非常費解。
后來經過一番分析,發現是庫文件夾里文件 tsconfig.json 引起的原因。
我把該文件的內容清空只剩下面一行,問題消失。
最后問題定位到,加了下圖這三行進行 paths 設置后,就出問題了:
"paths": {"@spartacus/core": ["dist/core"],"@spartacus/storefront": ["dist/storefrontlib"],"@spartacus/cart": ["dist/cart"]},每次 Visual Studio Code 里修改了 tsconfig.json 之后,都會顯示 Initializing Angular Language features:
目錄中存在 tsconfig.json 文件表明該目錄是 TypeScript 項目的根目錄。 tsconfig.json 文件指定了編譯項目所需的根文件和編譯器選項。
https://stackoverflow.com/questions/52147201/angular-6-declare-path-for-library-in-tsconfig-lib-json
Angular will use the tsconfig.json file, which is configured in the angular.json file in architect --> build --> options --> tsconfig. Whereas VS Code uses the tsconfig.json file, which is at the bottom of the workspace. You need to add the paths in both configurations to get both of them to work correctly, or change the project to use the base tsconfig.json file in the angular.json file.
The tsconfig.json file has a property that extends which will take another file as base and override everything which are declared in the tsconfig.lib.json file. So if you have paths declared in the tsconfig.lib.json file, then the paths are no longer needed in the tsconfig.json file.
更多Jerry的原創文章,盡在:“汪子熙”:
總結
以上是生活随笔為你收集整理的关于 Angular 项目类型为 library 的工程使用 tsconfig.json 的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 推特“蓝V认证”早期用户流失严重,超过一
- 下一篇: 奇富科技发布2022年ESG报告:扶持3