简体   繁体   English

如何在 WebStorm / PhpStorm 中启用自定义 Angular 库的自动导入?

[英]How to enable auto import for custom Angular library in WebStorm / PhpStorm?

I'm trying to create an Angular 9 library with some components and classes I use in different projects.我正在尝试创建一个 Angular 9 库,其中包含我在不同项目中使用的一些组件和类。 What I did:我做了什么:

  1. ng new my-lib-project --create-application=false
  2. cd my-lib-project
  3. ng generate library my-lib --prefix lb
  4. I created a test-class.ts file in projects/my-lib/src/lib/ with an easy LibTestClass definition (see below)我在projects/my-lib/src/lib/中创建了一个 test-class.ts 文件,其中包含一个简单的 LibTestClass 定义(见下文)
  5. I added the line export * from './lib/test-class';我添加了行export * from './lib/test-class'; to the public-api.ts file.到 public-api.ts 文件。
  6. I called ng build my-lib and copied the result to myOtherProject/node_modules/我调用ng build my-lib并将结果复制到myOtherProject/node_modules/

Problem: when I type const test = new LibTestClass();问题:当我输入const test = new LibTestClass(); in the app.component.ts file of the myOtherProject, PhpStorm does not show the import hint to easily add the import line for TestClass.在 myOtherProject 的 app.component.ts 文件中,PhpStorm 不显示导入提示,以便轻松添加 TestClass 的导入行。 How can I make this auto import work for my custom library?如何使此自动导入适用于我的自定义库?

Further information:更多信息:

  • IDE: PhpStorm 2020.1.1 IDE:PhpStorm 2020.1.1
  • OS: MacOS操作系统:MacOS
  • Angular: 9.1.6 Angular:9.1.6
  • my-lib is not released to npm my-lib 未发布到 npm

test-class.ts:测试类.ts:

export class LibTestClass {

  private test = '';

  constructor() {}
}

Thanks to @lena I found the answer: In order to enable import hints for your Angular library you have to install a (local) dependency to your library build.感谢@lena,我找到了答案:为了为您的 Angular 库启用导入提示,您必须为您的库构建安装一个(本地)依赖项。 In my case the solution is:就我而言,解决方案是:

npm install --save ../my-lib-project/dist/my-lib

This creates a link to the local build of your library and is updated automatically when you build your library.这将创建一个指向您的库的本地构建的链接,并在您构建您的库时自动更新。 I recommend to use ng build --watch in development.我建议在开发中使用ng build --watch

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

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