简体   繁体   English

我们如何使用 tsconfig“别名路径”导入 @ngModule 和其他定义

[英]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
  • app1 - DOES NOT WORK << why cant I use the alias-path defined in root tsconfig? APP1 -不工作<<为什么不能我用根tsconfig定义别名路径? ( "@lib1": ["projects/lib1/src/public-api"] ). ( "@lib1": ["projects/lib1/src/public-api"] )。 Its a combination of app2 and app4 .它是app2app4的组合。
  • The error message for app1: ` ERROR in projects/app1/src/app/app.component.ts:7:3 - error NG8001: 'lib-lib1' is not a known element: app1 的错误消息:` ERROR in projects/app1/src/app/app.component.ts:7:3 - error NG8001: 'lib-lib1' is not a known element:
    1. If 'lib-lib1' is an Angular component, then verify that it is part of this module.如果“lib-lib1”是一个 Angular 组件,则验证它是否是该模块的一部分。
    2. If 'lib-lib1' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.如果“lib-lib1”是一个 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“@NgModule.schemas”以抑制此消息。 (lib-lib1 component) ` (lib-lib1 组件)`
  • app2 - works - is the angular guided way. app2 -有效- 是角度引导方式。 It uses a tsconfig -> path onto the dist/lib1 (build library output)它使用tsconfig -> path到 dist/lib1(构建库输出)
  • app3 - works - uses relativ paths to the EXACT file location as you all have suggested app3 -有效- 使用您所建议的 EXACT 文件位置的相对路径
  • app4 - works - uses relativ paths to the 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。

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.

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