Angular 2 'component' is not a known element
Solution 1:
These are the 5 steps I perform when I get such an error.
- Are you sure the name is correct? (Also check the selector defined in the component)
- Declare the component in a module?
- If it is in another module, export the component?
- If it is in another module, import that module?
- Restart the cli?
When the error occurs during unit testing, make sure your declared the component or imported the module in TestBed.configureTestingModule
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.
You can't declare a component twice. You should declare and export your component in a new separate module. Next you should import this new module in every module you want to use your component.
It is hard to tell when you should create new module and when you shouldn't. I usually create a new module for every component I reuse. When I have some components that I use almost everywhere I put them in a single module. When I have a component that I don't reuse I won't create a separate module until I need it somewhere else.
Though it might be tempting to put all you components in a single module, this is bad for the performance. While developing, a module has to recompile every time changes are made. The bigger the module (more components) the more time it takes. Making a small change to big module takes more time than making a small change in a small module.
Solution 2:
I had a similar issue. It turned out that ng generate component
(using CLI version 7.1.4) adds a declaration for the child component to the AppModule, but not to the TestBed module that emulates it.
The "Tour of Heroes" sample app contains a HeroesComponent
with selector app-heroes
. The app ran fine when served, but ng test
produced this error message: 'app-heroes' is not a known element
. Adding the HeroesComponent
manually to the declarations in configureTestingModule
(in app.component.spec.ts
) eliminates this error.
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();
});
}
Solution 3:
I just had the exact same issue. Before trying some of the solutions posted here, you might want to check if the component really doesn't work. For me, the error was shown in my IDE (WebStorm), but it turned out that the code worked perfectly when i ran it in the browser.
After I shut down the terminal (that was running ng serve) and restarted my IDE, the message stopped showing up.
Solution 4:
A lot of answers/comments mention components defined in other modules, or that you have to import/declare the component (that you want to use in another component) in its/their containing module.
But in the simple case where you want to use component A
from component B
when both are defined in the same module, you have to declare both components in the containing module for B
to see A
, and not only A
.
I.e. in 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.
],
})
Solution 5:
I had the same problem with Angular CLI: 10.1.5 The code works fine, but the error was shown in the VScode v1.50
Resolved by killing the terminal (ng serve) and restarting VScode.