我试图在其他模块中使用我在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,在我的情况下,问题是在添加'import'语句后还没有保存文件。

其他回答

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

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

假设你有一个组件:

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 { }

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

我的步骤:

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

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

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

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

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

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