How do you load a component in ionic 4 after running the command ionic g component myComponent
? I want to add a new generated custom component to my home page.

- 1,684
- 3
- 20
- 36
-
3I actually have the exactly same issue, it's quite easy to reproduce as well. Just start a new project: ionic start project blank --type=angular. Then generate a component: ionic g component timer And finally add
to home.page.html It'll give you the "app-timer is not a known element" error. I've read through a lot of the v4 documentation to no avail. I realize this doesn't solve it, but I thought I'd provide a repro. EDIT: line breaks are missing, sorry about that. – Steffen Dec 08 '18 at 16:56
8 Answers
Finally figured this out, here's a repro that works:
ionic start myProject blank --type=angular
ionic g module components
ionic g component components/myComponent --export
This adds both a declaration and export to the components module for "myComponent".
To use the component just add ComponentsModule
to your imports
in your page module, e.g.
@NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
ComponentsModule,
RouterModule.forChild([
{
path: '',
component: HomePage
}
])
],
declarations: [HomePage]
})
export class HomePageModule { }
This way nothing is added to app.module.ts
which is how it should be.
Also note if you want to use ANY components in your own custom components, you need to add IonicModule
to the imports
in components.module.ts
Hope this helps :-)

- 13,648
- 7
- 57
- 67
-
2Somehow, it works with a blank new project, but in my existing project it loads an empty page with no errors reported. – alex351 Jan 03 '19 at 22:56
-
-
1If i want to use single component in any page..? Then also do i need to import whole componentModule to my page module? – Raj Feb 15 '19 at 10:07
-
4For some reason, the --export flag didn't work for me, but adding the MyComponentComponent to the ComponentsModule declarations and exports arrays manually worked. – garrettmills Sep 22 '19 at 22:13
-
With the comment from glmdev it works, but in the html the component shows "element not found". After I added the myComponent to EntryComponents in home.module.ts it works for me. – Sean Stayns Feb 04 '21 at 10:41
Basically, ionic g component myComponent
will update the app.module.ts and create the component inside the app folder.
But if you want a more elegant way to add the components. Here is the steps:
ionic g module components
will generate a module folder called components
. Then generate a bunch of components:
ionic g component components/myComponent --export
ionic g component components/myComponent2 --export
ionic g component components/myComponent3 --export
ionic g component components/myComponent4 --export
Inside components.module.ts could be write like this :
...
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { IonicModule } from '@ionic/angular';
import { MyComponentComponent } from './my-component/my-component.component';
import { MyComponentComponent2 } from './my-component2/my-component2.component';
import { MyComponentComponent3 } from './my-component3/my-component3.component';
import { MyComponentComponent4 } from './my-component4/my-component4.component';
@NgModule({
declarations: [
MyComponentComponent,
MyComponentComponent2,
MyComponentComponent3,
MyComponentComponent4
],
imports: [
CommonModule,
FormsModule,
IonicModule,
],
exports: [
MyComponentComponent,
MyComponentComponent2,
MyComponentComponent3,
MyComponentComponent4
]
})
export class ComponentsModule {}
and then make sure to import the components module inside the app.module.ts
:
...
import { ComponentsModule } from './components/components.module';
...
@NgModule({
declarations: [AppComponent],
imports: [
...
ComponentsModule,
...
],
providers: [
...
],
bootstrap: [AppComponent]
})
export class AppModule {}
To test the components, you need to create a page or component again.
ionic g page testing
Import components module into your testing component/page or (into your current home page similarly):
...
import { ComponentsModule } from '../components/components.module';
...
@NgModule({
imports: [
...
ComponentsModule,
...
],
declarations: [TestingPage]
})
export class TestingPageModule {}
Finally, just write the component inside the testing page by using the component selector. e.g.
<app-my-component></app-my-component>
<app-my-component2></app-my-component2>
<app-my-component3></app-my-component3>
<app-my-component4></app-my-component4>
Hope this might help.

- 2,316
- 2
- 20
- 26
This is your selector in components>foot-card>foot-card.ts:
import { Component } from '@angular/core';
@Component({
selector: 'foot-card',
templateUrl: 'foot-card.html'
})
export class FootCardComponent {
text: string;
constructor() {
console.log('Hello FootCardComponent Component');
this.text = 'Hello World';
}
}
This is your components.module.ts:
import { NgModule } from '@angular/core';
import { FootCardComponent } from './foot-card/foot-card';
import { IonicModule } from 'ionic-angular';
@NgModule({
declarations: [FootCardComponent],
imports: [IonicModule],
exports: [FootCardComponent]
})
export class ComponentsModule {}
This is your app.module.ts:
import { FootCardComponent } from '../components/foot-card/foot-card';
import { ComponentsModule } from '../components/components.module'
@NgModule({
declarations: [
],
imports: [
ComponentsModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
FootCardComponent
],
providers: [
]
})
export class AppModule {}
You have to import component-module in import and declare component-name in entry components in app.module.ts.
In components.module.ts, You have to declare and export the component but don't forget to import IonicModule.
I was facing the same problem but no one told to import IonicModule In Components.module.ts and In app.module.ts, only add to entryComponent and import componentModule.

- 4,313
- 1
- 24
- 32
After doing all the above ...
only worked in home.page.html adding app before the name of my component like :
<app-my-component></app-my-component>

- 4,313
- 1
- 24
- 32
The key is to include a new module, specifically for your custom component.
I just do not understand why the command "ionic generate component components/myComponent --export" no longer creates this outmoded module. I went through this same problem here.

- 1,018
- 12
- 28
Testes this solution on Ionic 6.11.0. Works perfectly. Flows to create a reusable component named 'FeaturedProduct' -
Step 1: Create a component ionic g component components/FeaturedProduct
Step 2: Create a module for this component.
ionic g module components/FeaturedProduct
Step 3: Import FeaturedProductComponent to featured-product.module.ts file
@NgModule({
declarations: [FeaturedProductComponent],
exports: [FeaturedProductComponent],
imports: [
CommonModule
]
})
Step 4: Import FeaturedProductModule on the module.ts file of your desired page.
@NgModule({
imports: [
...
...
FeaturedProductModule
],
declarations: [Tab2Page]
})
Now the component can be used by this tag
<app-featured-product></app-featured-product>

- 81
- 4
In your src/components folder
myComponent.html:
//Here your component HTML Code. For example:
<ion-list radio-group (ionSelect)="change($event, item.type);"
...
</ion-list>
components.module.ts:
import {myComponentComponent} from "./myComponent/myComponent";
@NGModule({
declatations: [myComponentComponent],
imports:[],
exports: [myComponentComponent]
})
export class ComponentsModule{}
In your src/app folder
app.module.ts:
import { MyComponentComponent } from "../components/myComponent/myComponent";
@NGModule({
declarations: [
yourPages....,
MyComponentComponent
]
)}
To use it:
For example in HomePage.html:
<myComponent> </myComponent>

- 37
- 2
-
1'mycomponent' is not a known element: 1. If 'mycomponent' is an Angular component, then verify that it is part of this module. 2. To allow any element add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. – Yushin Nov 07 '18 at 09:58
-
-
-
-
no please note that in ionic 4 its proving to be difficult try doing it – Yushin Nov 07 '18 at 10:51
-
I dont think that there is a different component installation in Ionic 4 than in Ionic 3. What is now the problem? – Jonathan Nov 07 '18 at 11:04
-
Jonathan > Ionic 4 doesn't handle components the same way as Ionic 3. Your solution works fine for Ionic 3, but not for Ionic 4. – Steffen Dec 08 '18 at 16:58
simply enter below command to genrate custom component
ionic g component components/Component-Name
add component to html

- 85
- 1