Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 2 Page Change Subscription

Tags:

ionic2

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?

like image 914
mark Avatar asked Nov 25 '25 01:11

mark


1 Answers

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

like image 81
Veerendra Borra Avatar answered Nov 28 '25 15:11

Veerendra Borra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!