[英]Using path mapping in tsconfig with Angular CLI
I want to use path-mapping to directly reference a library which sits in the same directory structure as my angular application. 我想使用路径映射直接引用与我的angular应用程序位于同一目录结构中的库。
Structure: 结构体:
mycompany-mylib/
src/
tsconfig.json
I've added the following to my tsconfig.json
我已将以下内容添加到
tsconfig.json
{
"compilerOptions": {
...
"baseUrl": ".",
"paths": {
"@mycompany/mylib": ["mycompany-mylib/lib/src/public_api"],
}
}
}
This configuration seems to be correct and Typescript can resolve these paths as I get no errors and full intellisense in Visual Studio Code. 此配置似乎是正确的,并且Typescript可以解析这些路径,因为我在Visual Studio Code中没有出现错误并获得了完整的智能感知。
For example: 例如:
import { UserClientModule } from '@mycompany/mylib'
However when I run ng serve
or ng serve --aot
I get an error, that the module was not found (My AngularCLI Version is 1.7.4 ): 但是,当我运行
ng serve
或ng serve --aot
,出现错误,未找到模块(我的AngularCLI版本为1.7.4 ):
ERROR in src/app/core/components/user-profile/user-profile.component.ts(5,43): error TS2307: Cannot find module '@mycompany/mylib'.
This technique should work as there are several blog posts and github issues referencing it. 该技术应该有效,因为有数篇博客文章和github问题都在引用它。 I just can't see what I'm doing wrong.
我只是看不到我在做什么错。 I can fix this in webpack by providing an alias configuraton, but since there is no
webpack.config.js
in angular cli, I can't do that. 我可以通过提供别名configuraton在webpack中解决此问题,但是由于angular cli中没有
webpack.config.js
,所以我无法做到这一点。
I was able to fix it myself. 我本人可以修复它。 Angular CLI, by default, uses another config:
./src/tsconfig.app.json
, I added my configuration there too and slightly modified the paths like this: 默认情况下,Angular CLI使用另一个配置:
./src/tsconfig.app.json
,我./src/tsconfig.app.json
添加了配置,并对路径进行了如下修改:
{
"compilerOptions": {
...
"baseUrl": "./",
"paths": {
"@mycompany/mylib": ["../mycompany-mylib/lib/src/public_api"],
}
}
}
It now compiles fine, as long is there is not a node_modules
folder in mycompany-mylib
. 现在只要
mycompany-mylib
没有node_modules
文件夹,它就可以正常编译。 I would still thankfull for a solution to this. 我仍然要感谢对此的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.