我试图在其他模块中使用我在AppModule中创建的组件。我得到以下错误:

Uncaught (in promise):错误:模板解析错误 'contacts-box'不是已知元素: 如果'contacts-box'是一个Angular组件,那么验证它是否是这个模块的一部分。 如果'contacts-box'是一个Web组件,那么在'@NgModule '中添加'CUSTOM_ELEMENTS_SCHEMA'。模式来抑制此消息。

我的项目结构很简单:

我将页面保存在pages目录中,其中每个页面保存在不同的模块中(例如customers-module),每个模块都有多个组件(例如customers-list-component, customers-add-component等)。我想在这些组件中使用我的ContactBoxComponent(例如在customers-add-component内部)。

As you can see I created the contacts-box component inside the widgets directory so it's basically inside the AppModule. I added the ContactBoxComponent import to app.module.ts and put it in declarations list of AppModule. It didin't work so I googled my problem and added ContactBoxComponent to export list as well. Didn't help. I also tried putting ContactBoxComponent in CustomersAddComponent and then in another one (from different module) but I got an error saying there are multiple declarations.

我错过了什么?


当前回答

在执行Angular的入门教程时,我也在使用stackblitz.com工具使用Angular生成器生成一个名为product-alerts的新组件后收到了这个错误。

'app-product-alerts'不是已知元素:

发现:其他人在2021年8月初也经历过这种情况。目前,它似乎是StackBlitz IDE中的一个bug。 https://github.com/angular/angular/issues/43020

其他回答

在我的例子中,我的应用程序有多层模块,所以我试图导入的模块必须添加到实际使用它的父模块pages.module。Ts,而不是app。module。Ts。

这个问题可能看起来古老而奇怪,但是当我试图加载一个模块(惰性加载)并得到相同的错误时,我意识到我错过了作为一个大模块的一部分发布的组件的exports子句。

这个角。Link解释了原因:模块内的组件/服务,默认情况下保持私有(或受保护)。要使它们公开,您必须导出它们。

扩展@Robin Djikof的回答,用@live-love代码示例,这是我的案例中技术上缺失的部分(Angular 8):

@NgModule({
  declarations: [
    SomeOtherComponent,
    ProductListComponent
  ],
  imports: [
    DependantModule
  ],
  exports: [ProductListComponent] 
  //<- This line makes ProductListComponent available outside the module, 
  //while keeping SomeOtherComponent private to the module
})
export class SomeLargeModule { }

我也遇到过类似的问题。事实证明,ng generate component(使用CLI 7.1.4版)为AppModule添加了子组件的声明,但没有添加到模拟它的TestBed模块。

“英雄之旅”示例应用程序包含一个HeroesComponent,它带有选择器app- Heroes。应用程序运行正常时,但ng test产生这个错误消息:'app-heroes'不是一个已知元素。手动将HeroesComponent添加到configureTestingModule(在app.component.spec.ts中)的声明中可以消除这个错误。

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent,
        HeroesComponent
      ],
    }).compileComponents();
  }));

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  });
}

路由模块(不认为这是一个答案)

首先检查:是否在模块中声明并导出了组件,导入了想要使用它的模块,并在HTML中正确命名了组件。

否则,你可能会错过路由模块中的一个模块: 当您有一个路由模块,该路由从另一个模块路由到一个组件时,在该路由模块中导入该模块是很重要的。否则,Angular CLI会显示错误:component不是已知元素。

例如

1)具有以下项目结构:

├───core
│   └───sidebar
│           sidebar.component.ts
│           sidebar.module.ts
│
└───todos
    │   todos-routing.module.ts
    │   todos.module.ts
    │
    └───pages
            edit-todo.component.ts
            edit-todo.module.ts

2)在todos-routing.module内部。Ts你有一个到edit.todo.component.ts的路径(不导入它的模块):

  {
    path: 'edit-todo/:todoId',
    component: EditTodoComponent,
  },

这条路线会很好!但是,当在edit-todo.module中导入sidebar.module.ts时。你会得到一个错误:app-sidebar不是一个已知的元素。

修正:由于您已经在步骤2中添加了到edit-todo.component.ts的路由,您将必须添加edit-todo.module。Ts作为导入,之后导入的侧边栏组件将工作!

我知道这是一个长期解决的问题,但在我的情况下,我有一个不同的解决方案。 这实际上是我犯的一个错误,也许你也犯了,只是没有完全注意到。我的错误是,我不小心把一个模块,例如MatChipsModule, MatAutoCompleteModule,在声明部分;只由组件组成的部分,例如MainComponent, AboutComponent。这使我其他所有的进口货都认不出来了。