我试图在其他模块中使用我在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.

我错过了什么?


当我得到这样的错误时,我执行以下5个步骤。

你确定名字是正确的吗?(还要检查组件中定义的选择器) 在模块中声明组件? 如果它在另一个模块中,导出组件? 如果它在另一个模块中,导入该模块? 重启cli?

当错误发生在单元测试期间,请确保您在TestBed.configureTestingModule中声明了组件或导入了模块

我也尝试把ContactBoxComponent在CustomersAddComponent,然后在另一个(从不同的模块),但我得到了一个错误,说有多个声明。

一个组件不能声明两次。您应该在一个新的单独模块中声明和导出组件。接下来,您应该将这个新模块导入到希望使用该组件的每个模块中。

很难说什么时候应该创建新模块,什么时候不应该。我通常为我重用的每个组件创建一个新模块。当我有一些几乎无处不在的组件时,我把它们放在一个模块中。当我有一个我不重用的组件时,我不会创建一个单独的模块,直到我在其他地方需要它。

尽管将所有组件放在单个模块中可能很诱人,但这对性能不利。在开发过程中,每当进行更改时,模块都必须重新编译。模块越大(组件越多)所花费的时间就越多。对大模块做小改动要比对小模块做小改动花费更多的时间。


我也有同样的问题。在尝试这里发布的一些解决方案之前,您可能需要检查组件是否真的不能工作。对我来说,这个错误显示在我的IDE (WebStorm)中,但当我在浏览器中运行它时,结果证明代码完美地工作了。

在我关闭终端(正在运行ng serve)并重新启动IDE之后,该消息停止显示。


我有相同的问题宽度php风暴版本2017.3。这为我解决它: Intellij支持论坛

它是一个错误宽度@angular语言服务: https://www.npmjs.com/package/@angular/language-service


我也遇到过类似的问题。事实证明,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();
  });
}

我也遇到了同样的问题,这是因为不同的功能模块错误地包含了这个组件。当把它从其他功能中移除时,它工作了!


在我的例子中,问题是模块中缺少组件声明,但即使添加了声明,错误仍然存在。我不得不停止服务器并在VS Code中重新构建整个项目以消除错误。


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


这个复杂的框架快把我逼疯了。假设你在同一个模块的另一个组件部分的模板中定义了自定义组件,那么你不需要在模块中使用导出(例如app.module.ts)。你只需要在前面提到的模块的@NgModule指令中指定声明:

// app.module.ts

import { JsonInputComponent } from './json-input/json-input.component';

@NgModule({
  declarations: [
    AppComponent,
    JsonInputComponent
  ],
  ...

你不需要将JsonInputComponent(在本例中)导入到AppComponent(在本例中)来使用AppComponent模板中的JsonInputComponent自定义组件。你只需要在自定义组件前加上两个组件都定义过的模块名(例如app):

<form [formGroup]="reactiveForm">
  <app-json-input formControlName="result"></app-json-input>
</form>

注意app-json-input不是json-input!

演示在这里:https://github.com/lovefamilychildrenhappiness/AngularCustomComponentValidation


我正在开始使用Angular,在我的情况下,问题是在添加'import'语句后还没有保存文件。


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

首先检查:是否在模块中声明并导出了组件,导入了想要使用它的模块,并在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作为导入,之后导入的侧边栏组件将工作!


我也面临着同样的问题。在我的情况下,我已经忘记在声明父组件 app.module.ts

举个例子,如果你在ToDayComponent模板中使用<app-datapicker>选择器,你应该在app.module.ts中声明ToDayComponent和DatepickerComponent


假设你有一个组件:

product-list.component.ts:

import { Component } from '@angular/core';
    
    @Component({
        selector: 'pm-products',  
        templateUrl: './product-list.component.html'
    })
    
    
    export class ProductListComponent {
      pageTitle: string = 'product list';
    }

你会得到这个错误:

在src/app/app.com ponponts:6:3 -错误NG8001: 'pm-products' 不是已知元素: 如果'pm-products'是一个Angular组件,那么验证它是这个模块的一部分。

app.component.ts:

import { Component } from "@angular/core";
@Component({
  selector: 'pm-root', // 'pm-root'
  template: `
  <div><h1>{{pageTitle}}</h1>
  <pm-products></pm-products> // not a known element ?
  </div>
  `
})
export class AppComponent {
  pageTitle: string = 'Acme Product Management';
}

确保你导入了组件:

app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';

// --> add this import (you can click on the light bulb in the squiggly line in VS Code)
import { ProductListComponent } from './products/product-list.component'; 

@NgModule({
  declarations: [
    AppComponent,
    ProductListComponent // --> Add this line here

  ],
  imports: [
    BrowserModule
  ],
  bootstrap: [AppComponent],


})
export class AppModule { }

这个问题可能看起来古老而奇怪,但是当我试图加载一个模块(惰性加载)并得到相同的错误时,我意识到我错过了作为一个大模块的一部分发布的组件的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 { }

很多答案/注释提到了在其他模块中定义的组件,或者你必须在它/它们的包含模块中导入/声明组件(你想在另一个组件中使用)。

但在简单的情况下,当组件B和组件A都定义在同一个模块中时,你必须在包含组件的模块中声明两个组件,以便B看到A,而不仅仅是A。

例如,在my-module.module.ts中

import { AComponent } from "./A/A.component";
import { BComponent } from "./B/B.component";

@NgModule({
  declarations: [
    AComponent,   // This is the one that we naturally think of adding ..
    BComponent,   // .. but forget this one and you get a "**'AComponent'** 
                  // is not a known element" error.
  ],
})

我在Angular CLI: 10.1.5中也遇到了同样的问题 代码运行正常,但错误显示在VScode v1.50中

通过杀死终端(ng serve)并重新启动VScode来解决。


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


我花了半天时间来解决这个问题。问题出在进口方面。我的HomeModule有html中包含的homeComponent。ProductComponent是ProductModule的一部分。我在导入中添加了ProductModule到HomeModule,但忘记在导入中添加HomeModule到AppModule。添加后,问题消失了


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

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

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


我的情况有点不同。我得到了这个错误消息,但这是因为我的页面模块没有我生成的新页面。

我的步骤:

生成新的页面组件 将现有组件导入到新页面的模块中。ts文件 在新页面的HTML文件中使用<myapp-header></myapp-header>组件选择器

得到了错误,被困在这里。对我来说,最后一步是:

导入新的页面模块到pages.module.ts文件中。

现在它按预期工作了。这次错误消息没有太大帮助。


对于我的应用程序模式,一旦我在app.module.ts文件中声明了我的LandingPageComponent,就可以将子模块导入其中。

我刚开始一个新项目,并认为其中一些模式是理所当然的。


我有一个类似的问题,但没有一个提到的解决方案工作。最后我意识到我有一个路由问题:

我想如果我在应用程序路由中指定我的组件。模块如下:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { MainComponent } from './pages/main/main.component';

const routes: Routes = [
  {
    path: '', 
    component: MainComponent
  },
]

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

我将不必在我的AppModule中导入MainModule,这是错误的,因为这里只有组件被导入。MainModule中声明的任何其他组件将不可见,这将导致您所描述的错误消息。

解决方案1: 导入声明模块 解决方案2: 惰性加载组件,如下所示:

const routes: Routes = [
{
    path: '', loadChildren: () => import('path_to_your_module').then(m => m.MainModule)},
}

这是可行的,因为整个模块都导入到这里。

我意识到你在你的例子中没有使用路由,只是把这个发布给其他可能使用路由的人,并无意中发现了这个问题。