When I run this command:
ionic generate component my-component
It creates the following folder structure:
components
my-component
my-component.ts
my-component.scss
my-component.html
components.module.ts
Now, I want to incorporate this component into a page. While I am not set on using Ionic 3's lazy-loading feature, I am open to it if it simplifies the process. My page does not have its own module, and here is the folder structure for the page:
pages
somepage
somepage.ts
somepage.scss
somepage.html
The component template also includes custom ionic elements (ion-grid). The code in my-component.html
looks like this:
<ion-grid></ion-grid>
However, this line is highlighted in red in VS Code with the error message stating:
'ion-grid' is not a known element:
1. If 'ion-grid' is an Angular component, verify that it is part of this module.
2. If 'ion-grid' is a Web Component, add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this error.
To resolve this issue, many sources suggest adding IonicModule
and CommonModule
into the components.module.ts
:
@NgModule({
declarations: [MyComponent],
imports: [
CommonModule ,
IonicModule
],
exports: [MyComponent]
})
export class ComponentsModule {}
Despite implementing these changes, the error persists.
Another problem arises where the page fails to recognize the new custom element. This line in somepage.html
:
<my-component></my-component>
is also flagged in red indicating the error:
'my-component' is not a known element:
1. If 'my-component' is an Angular component, ensure it is included in this module.
2. If 'my-component' is a Web Component, add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
Initially, I attempted to add the Components module to the app.module.ts
:
@NgModule({
declarations: [
MyApp,
SomePage
],
imports: [
...
ComponentsModule
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
SomePage
],
providers: [...]
})
export class AppModule {}
Unfortunately, this did not resolve the issue. After hours of researching, I discovered that the components.module.ts
is primarily designed for lazy loading, prompting me to consider creating a module for the page instead. Frustrated, I attempted adding the component directly to the app.module.ts
:
@NgModule({
declarations: [
MyApp,
SomePage,
MyComponent
],
imports: [
...
MyComponent
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
SomePage
],
providers: [...]
})
export class AppModule {}
Various other configurations were tested as well, but none proved successful. Using a component in Angular should not be this challenging, especially considering how straightforward it was in Ionic 2. What steps are necessary to utilize a component in Ionic 3?