简体   繁体   English

导入定制的 Angular 库时出现 StaticInjectorError

[英]StaticInjectorError when importing a custom-made Angular library

I've built my own Angular library (called ProcessorModule) and want to use it in other applications.我已经构建了自己的 Angular 库(称为 ProcessorModule)并希望在其他应用程序中使用它。 Whenever I import the library however, I get the following error:但是,每当我导入库时,都会出现以下错误:

StaticInjectorError(AppModule)[StatusComponent -> MainProcessComponent].
NullInjectorError: No provider for MainProcessComponent!

This occurs within the html of MainProcessComponent which has a nested StatusComponent (specifically when I use the 'status' selector:这发生在具有嵌套 StatusComponent 的 MainProcessComponent 的 html 中(特别是当我使用“状态”选择器时:

/* MainProcessComponent.html */
<h2>This is the MainProcessComponent view</h2>

<div>
    <status></status>
</div>

This is my processor.module.ts file:这是我的 processor.module.ts 文件:

import { NgModule } from '@angular/core';
import { CommonModule } from "@angular/common";
import { FormsModule } from "@angular/forms";
import { HttpClientModule } from "@angular/common/http";

import { MainProcessComponent } from "./components/process.component";
import { StatusComponent } from "./components/status.component";

@NgModule({
  imports: [
    routing,
    CommonModule,
    FormsModule,
    HttpClientModule
  ],
  exports: [
    MainProcessComponent,
    ProcessComponent,
    StatusComponent
  ],
  declarations: [
    MainProcessComponent,
    ProcessComponent,
    StatusComponent
  ],
  bootstrap: [

  ]
})
export class ProcessorModule { }

I build/pack a.tgz file for ProcessorModule and import it into my outside application using我为 ProcessorModule 构建/打包 a.tgz 文件并将其导入到我的外部应用程序中

import { ProcessorModule} from "processor-module";

Within the imports section of @NgModule I add:@NgModule的导入部分中,我添加:

@NgModule({
    imports: [
        ...,
        ProcessorModule,
        ...
    ]
})
export class OutsideAppModule { }

Keep in mind the above code has been simplified to help illustrate my issue.请记住,上面的代码已被简化以帮助说明我的问题。 I thought adding both components to the exports section of processor.module.ts would have solved my problem.我认为将这两个组件添加到processor.module.ts的导出部分会解决我的问题。 Can anyone see what exactly I'm doing wrong?谁能看到我到底做错了什么?

EDIT: I've added MainProcessComponent to both the 'exports' and 'declarations' section of processor.module.ts but I still get the same error.编辑:我已将 MainProcessComponent 添加到 processor.module.ts 的“exports”和“declarations”部分,但我仍然遇到相同的错误。

MainProcessComponent is not exported in processor.module.ts 's exports array. MainProcessComponent未在processor.module.ts的导出数组中导出。

processor.module.ts处理器模块.ts

@NgModule({
  imports: [..],
  exports: [
    ProcessComponent,
    StatusComponent,
    MainProcessComponent // added
  ],
  declarations: [...],
  bootstrap: [... ]
})
export class ProcessorModule { }

Figured out my problem - I had some components declared in the constructor of another component (not shown above) that were unused.找出我的问题 - 我在另一个未使用的组件(上面未显示)的构造函数中声明了一些组件。 After removing them everything worked fine.删除它们后一切正常。

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

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