Currently, I am encountering an issue with importing a component into another in my Ionic 5.0.0 application.
Within my application, I have two separate modules: ChatPageModule and HomePageModule. My objective is to include the Chat template within the Home template (similar to ng-include) so that the home screen displays both templates simultaneously on the left and right sides.
To achieve this, I created a new module named SharedPageModule:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { Routes, RouterModule } from '@angular/router';
import { IonicModule } from '@ionic/angular';
import { ChatPage } from '../chat/chat.page';
const routes: Routes = [
];
@NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
RouterModule.forChild(routes)
],
declarations: [ChatPage],
exports: [ChatPage]
})
export class SharedPageModule {}
Following that, I integrated the SharePageModule into the HomePageModule as shown below:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { IonicModule } from '@ionic/angular';
import { FormsModule } from '@angular/forms';
import { RouterModule } from '@angular/router';
import { HomePage } from './home.page';
import { SharedPageModule } from '../shared/shared.module'
@NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
RouterModule.forChild([
{
path: '',
component: HomePage
}
]),
SharedPageModule
],
declarations: [HomePage]
})
export class HomePageModule {}
I proceeded by adding the template selector of the Chat component to the home template like so:
<ion-header>
<ion-toolbar>
<ion-title text-center>HOME</ion-title>
</ion-toolbar>
</ion-header>
<ion-content class="homepage-content no-scroll" >
<ion-row>
...
</ion-row>
<ion-row>
...
</ion-row>
<ion-row>
<app-chat></app-chat> <!-- Included here -->
</ion-row>
</ion-content>
Everything went well up to this point, but then I encountered an issue.
I intended to call some methods from the ChatPage component within the HomePage component. To achieve this, I imported the Chat component in Home in the following manner:
import { Platform } from '@ionic/angular';
import { ChatPage } from '../chat/chat.page'
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage implements OnInit {
constructor(private chat: ChatPage) {}
ngOnInit(): void {
this.chat.getMessages();
}
}
However, upon navigating to my home page, I encountered the error detailed below:
core.js:15724 ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[HomePage -> ChatPage]:
StaticInjectorError(Platform: core)[HomePage -> ChatPage]:
NullInjectorError: No provider for ChatPage!
Error: StaticInjectorError(AppModule)[HomePage -> ChatPage]:
StaticInjectorError(Platform: core)[HomePage -> ChatPage]:
NullInjectorError: No provider for ChatPage!
at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get (core.js:8896)
at resolveToken (core.js:9141)
at tryResolveToken (core.js:9085)
at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:8982)
at resolveToken (core.js:9141)
at tryResolveToken (core.js:9085)
at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:8982)
at resolveNgModuleDep (core.js:21218)
at NgModuleRef_.push../node_modules/@angular/core/fesm5/core.js.NgModuleRef_.get (core.js:21907)
at resolveNgModuleDep (core.js:21218)
at resolvePromise (zone.js:831)
at resolvePromise (zone.js:788)
at zone.js:892
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
at Object.onInvokeTask (core.js:17290)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:422)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:195)
at drainMicroTaskQueue (zone.js:601)