How can I subscribe to page changes in Ionic 2? I'd just like to log page names/titles globally. Is there an event I can subscribe to?
create a service
import {Injectable} from '@angular/core';
import {Subject} from 'rxjs/Rx';
@Injectable()
export class RouteNamesService{
constructor(){
}
public name = new Subject();
setRouteName(name){
this.name.next(name);
}
}
in app.component.ts subscribe to
this.routeNamesService.name.subscribe(name => this.routeName = name);
when you enter in to a component then fire
this.routeNamesService.setRouteName("Settings");
(or) in app.component.ts
@ViewChild(Nav) nav: Nav;
ngAfterViewInit() {
this.nav.viewDidEnter.subscribe((data) => {
console.log(data);
});
}
in data you can get component name
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