I have an ionic 5.0.0 application I'm facing an issue in importing a component into another.
I have 2 different modules in my application which are ChatPageModule and HomePageModule. I want to include Chat template in Home template (like ng-include). So my home screen will be split in 2. Left side will render home template and right side will render chat template simultaneously.
To achieve this I created a SharedPageModule as follows.
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 {}
After that, I imported the SharePageModule into HomePageModule as follows
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 {}
After I added the template selector of my Chat component into home template as follows.
<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> <!-- Here it is -->
</ion-row>
</ion-content>
Up to this everything was fine. now the issue starts.
I want to call some methods written in ChatPage componennt from HomePage component. To do this I imported the Chat component in Home as follows.
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();
}
}
But I got below error when i navigate to my home page.
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)