I\'m trying to use a component I created inside the AppModule in other modules. I get the following error though:
\"Uncaught (in promise): Error: Temp
I was facing the same issue. In my case I have forgotten to declare Parent component in the app.module.ts
As a example if you are using <app-datapicker>
selector in ToDayComponent
template, you should declare both ToDayComponent
and DatepickerComponent
in the app.module.ts
I am beginning Angular and in my case, the issue was that I hadn't saved the file after adding the 'import' statement.
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();
});
}
These are the 5 steps I perform when I got such an error.
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.
I have the same issue width php storm version 2017.3. This fix it for me: intellij support forum
It was an error width @angular language service: https://www.npmjs.com/package/@angular/language-service
This question may seem old and odd, but when I was trying to load a module(lazy loading) and getting the same error, I realized I was missing an exports clause for the component that shipped as a part of a larger module.
This Angular.io Link explains why: Components/Services inside a module, remains private(or protected) by default. To make them public, you have to export them.
Expanding on @Robin Djikof's answer with @live-love code sample, this is what was technically missing in my case(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 { }