I have a ionic-2 header bar containing the home or logout button and company logo which is common for all the pages. How do i write a common function(@Injectable()), so that it will be very easy to include in all the pages instead of repeating the code.
<ion-header>
<ion-navbar class="hide-border toolbar-btn-color" id="radio">
<button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title>Send Money</ion-title>
<ion-buttons end>
<button (click)="goHome()">
<ion-icon ios="ios-home" md="md-home"></ion-icon>
</button>
</ion-buttons>
</ion-navbar>
</ion-header>
In the every typescript file, i am repeating the function called gohome(). Is there any way to avoid this one?
get('ACCESS_TOKEN'); var headers = new Headers(); headers. append('Accept', 'application/json'); headers. append('Content-Type', 'application/json'); headers. append('Authorization', 'Bearer ' + token); headers.
You can do this by making header-component with configuration and then change configuration on different components accordingly.
Create custom header component.
Lets Call it 'custom-header-component'
custom-header-component.html
<ion-navbar>
<button *ngIf="header_data.ismenu" ion-button icon-only menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<button *ngIf="header_data.ishome" class="algo-header-home-icon" ion-button icon-only (click)="homeClick()">
<ion-icon class="ion-home" name="home"></ion-icon>
</button>
<ion-title class="header-title" [ngClass]="{'ishome-title': header_data.ishome,'ismeun-centre': !header_data.ishome}">
{{header_data.title}}
</ion-title>
</ion-navbar>
custom-header-component.ts
import { Component,Input } from '@angular/core';
import { NavController } from 'ionic-angular';
import { HomePage } from '../../pages/home/home';
@Component({
selector: 'custom-header',
templateUrl: 'custom-header.html'
})
export class CustomHeaderComponent {
header_data: any;
constructor(public navCtrl: NavController) {}
@Input()
set header(header_data: any) {
this.header_data=header_data;
}
get header() {
return this.header_data;
}
homeClick() {
this.navCtrl.setRoot(HomePage);
}
}
This custom-header-component takes configurations 'ismenu' of type boolean, 'ishome' of type boolean and 'title' of type string. Now lets use this component in page component 'home'. We want home page component to only have title and ismenu. Our code looks like this.
'home.html'
<ion-header><custom-header [header]="header_data"></custom-header></ion-header>
<ion-content padding class="page-home">
<p>Home Page</p>
</ion-content>
'home.ts'
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
header_data:any;
constructor(public navCtrl: NavController) {
this.header_data={ismenu:true,ishome:false,title:"Home"};
}
}
<br/><br/>
Home page header would look like this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With