最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why Am I Getting Angular Error NG8001 Not A Known Element? - Stack Overflow

programmeradmin4浏览0评论

I'm trying to figure out why I keep getting this error message when running ng serve. I'm new to Angular so I'm not sure where the problem is happening, but I will lay out what I did so maybe that helps to find a solution.

  1. `ng new project-name
  2. `ng generate component component-name
  3. Insert component selector into root component html file
  4. ng serve
  5. Error NG8001

The only answer I keep finding is that the new component has to be imported into the root component's imports, but when I generated the component in the CLI it is automatically imported and added to the imports. I got the error to go away by deleting and re-entering the import statement and imports array, but I'm curious as to why this error is happening when the CLI ng g c component-name is already doing that to begin with.

I'm using Angular 17 and standalone components

I'm trying to figure out why I keep getting this error message when running ng serve. I'm new to Angular so I'm not sure where the problem is happening, but I will lay out what I did so maybe that helps to find a solution.

  1. `ng new project-name
  2. `ng generate component component-name
  3. Insert component selector into root component html file
  4. ng serve
  5. Error NG8001

The only answer I keep finding is that the new component has to be imported into the root component's imports, but when I generated the component in the CLI it is automatically imported and added to the imports. I got the error to go away by deleting and re-entering the import statement and imports array, but I'm curious as to why this error is happening when the CLI ng g c component-name is already doing that to begin with.

I'm using Angular 17 and standalone components

Share Improve this question edited Mar 17 at 9:26 Brad Richerson asked Mar 17 at 9:23 Brad RichersonBrad Richerson 193 bronze badges 1
  • Are you inserting your component in the index.html file or appponent.html ? – RomainG Commented Mar 17 at 9:26
Add a comment  | 

2 Answers 2

Reset to default 0

You have to import the component to use the component in another component.

@Component({
  selector: 'app-parent',
  imports: [ComponentNameComponent], // <- add the generated component import here in order to use it in the HTML of parent!
  ...
})
export class ParentComponent {
  ...

I faced the same issue when I first worked with standalone components in Angular. Suppose the component you have generated is named ChildComponent. In the root component TS file, you have to import the ChildComponent and also include it in the imports array.

It should look like this :

import { Component } from '@angular/core';
import { ChildComponent } from './child/childponent';// import statement

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [ChildComponent],  //imports array
  templateUrl: './appponent.html',
  styleUrl: './appponent.scss'
})
发布评论

评论列表(0)

  1. 暂无评论