Following the advice of @Austin, it is recommended to encapsulate your logic within an injectable service
:
import {Injectable} from "@angular/core";
@Injectable()
export class NotificationService {
constructor(...) {
// ...
}
showNotification() {
//....
}
}
Next, include this service in the ionicBootstrap
function within your app.ts
file to ensure that the same instance of the service is utilized throughout the entire application.
ionicBootstrap(MyApp, [NotificationService], {});
Lastly, you can access and use this service in any desired component
by following these steps:
import {Component} from '@angular/core';
import {NotificationService} from './notificationService';
@Component({
templateUrl: 'build/test.html'
})
export class TestPage {
constructor(private notificationService: NotificationService) {
// ...
}
displayMessage() {
// Utilize the service to display the message
this.notificationService.showNotification();
}
}