[英]How can we import an @ngModule and other definitions with a tsconfig "alias path"
repo: https://github.com/andreElrico/mono-repo-test回购: https : //github.com/andreElrico/mono-repo-test
stackblitz: https://stackblitz.com/github/andreElrico/mono-repo-test (noop; only to navigate smoothly) stackblitz: https ://stackblitz.com/github/andreElrico/mono-repo-test(noop;只是为了顺利导航)
Let's assume the following structure:让我们假设以下结构:
root/
├── projects/
│ ├── app1
│ ├── app2
│ ├── app3
│ ├── app4
│ ├── lib1
"@lib1": ["projects/lib1/src/public-api"]
). ( "@lib1": ["projects/lib1/src/public-api"]
)。 Its a combination of app2
and app4
.它是app2
和app4
的组合。tsconfig -> path
onto the dist/lib1 (build library output)它使用tsconfig -> path
到 dist/lib1(构建库输出)public_api.ts
. APP4 -作品-用途relativ路径的public_api.ts
。 Why cant app1
not be compiled successfully?为什么app1
不能编译成功? Thats the case I want to work with?这就是我想要合作的情况? Path wise it should all be correct :-/ ?路径明智它应该都是正确的:-/? The reason behind why I want to make app1
work is that I want to pull request on a library and a "test-app" already has all imports "library style" => (eg. @lib1).我想让app1
工作的原因是我想在库上拉取请求,并且“测试应用程序”已经拥有所有导入“库样式”=>(例如@lib1)。 So its would be awesome to map the "normally" nodemodule path of "@lib1" to the exact sources of the library that are just siblings of this app.因此,将“@lib1”的“正常”节点模块路径映射到该应用程序的同级库的确切来源会很棒。 With that I can very fast debug and develop a new feature.有了它,我可以非常快速地调试和开发新功能。 Of course I would remove @lib1 from package.json fo the "test-app".当然,我会从“test-app”的 package.json 中删除@lib1。
You have added the module into the providers:您已将该模块添加到提供程序中:
https://github.com/andreElrico/mono-repo-test/blob/6939e4a36cd64a2f9df8e8abc8e3addf162a75cf/projects/app1/src/app/app.module.ts?fbclid=IwAR14_D6YilhHEsNep7azHpHPrGcMDlVAruDQa7dYDBIsBlkdGFaFyW-u-LE#L15 https://github.com/andreElrico/mono-repo-test/blob/6939e4a36cd64a2f9df8e8abc8e3addf162a75cf/projects/app1/src/app/app.module.ts?fbclid=IwAR14_D6YilhHEsDBYpHLazNepYdFuLazGDFLypHazNep7dFlugFdLyphLazNep77
If you add the module to the imports array it should work as expected.如果将模块添加到导入数组,它应该会按预期工作。
If your module is in the apps/app1
directory and the shared component you want to import is in sharedLibs/lib1
;如果你的模块在apps/app1
目录下,而你要导入的共享组件在sharedLibs/lib1
; just do:做就是了:
import { component } from "../../sharedLibs/lib1"
You can build a system that has a lot of up and down directory traversals this approach.你可以用这种方法构建一个有很多上下目录遍历的系统。 My preferred approach would be a separate library in a private npm repo for anything beyond a demo project.我的首选方法是在私有 npm 存储库中创建一个单独的库,用于演示项目之外的任何内容。
Edit :编辑:
To access the library through an alias without traversing the path structure;通过别名访问库,无需遍历路径结构; you should be able to edit the tsconfig.json
file.您应该能够编辑tsconfig.json
文件。
Look for compilerOptions.paths:寻找 compilerOptions.paths:
"paths": {
"lib1/*": ["../../sharedLibs/lib1/*"],
"lib2/*": ["../../sharedLibs/lib2/*"],
"lib3/*": ["../../sharedLibs/lib3/*"]
}
These paths should be looked at for library projects before the node_modules
is.应该在node_modules
之前查看库项目的这些路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.