Looking to streamline my headers in my Ionic 4 project by creating a separate reusable component for them.
Here's how I set up my dashboard page with the header component:
<ion-header>
<app-header title="Dashboard"></app-header>
</ion-header>
This is the code for my header.component.html:
<ion-header class="test">
<ion-toolbar class="header-background-color">
<ion-buttons slot="start">
<ion-menu-button></ion-menu-button>
</ion-buttons>
<ion-title id="dashboaerdheadline">
{{lablesHeadlines.dashboard}}
</ion-title>
</ion-toolbar>
</ion-header>
In my dashboard.module.ts file, I included the HeaderComponent:
import { NgModule } from '@angular/core';
import { CommonModule } from '@common/common';
import { FormsModule } from '@forms/forms';
import { IonicModule } from '@ionic/angular';
import { DashboardPageRoutingModule } from './dashboard-routing.module';
import { DashboardPage } from './dashboard.page';
import { MapComponent } from '../map/map.component';
import { HeaderComponent } from '../header/header.component';
@NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
DashboardPageRoutingModule
],
declarations: [DashboardPage, MapComponent, HeaderComponent]
})
export class DashboardPageModule {}
After starting my app, I encountered the following errors:
Error messages displayed in the console:
ERROR TypeError: Cannot read properties of undefined (reading 'dashboard')
...
Any advice on what might be causing this issue and how to resolve it? I'm fairly new to Angular/Ionic development.
UPDATE 1 - environment lables.ts file:
export const lablesHeadlines = {
dashboard: 'Map',
};
UPDATE 2 - Header component TypeScript file:
import { Component, OnInit } from '@angular/core';
import { lablesHeadlines } from 'src/environments/lables';
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.scss'],
})
export class HeaderComponent implements OnInit {
constructor() { }
ngOnInit() {}
}
UPDATE 3 - More detailed error message:
ERROR TypeError: Cannot read properties of undefined (reading 'dashboard')
...
UPDATE 4 - Updated HTML template for the HeaderComponent:
1 <ion-header class="test">
2 <ion-toolbar class="header-background-color">
3 <ion-buttons slot="start">
4 <ion-menu-button></ion-menu-button>
5 </ion-buttons>
6 <ion-title id="dashboaerdheadline">
7 {{headlines.dashboard}}
8 </ion-title>
9 </ion-toolbar>
10 </ion-header>