Ionic 2 Page Change Subscription

时间:2017-04-08 20:56:31

标签: ionic2

如何订阅Ionic 2中的页面更改?我只是想全局记录页面名称/标题。我可以订阅一个活动吗?

1 个答案:

答案 0 :(得分:2)

创建服务

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);
  }
}
app.component.ts中的

订阅

 this.routeNamesService.name.subscribe(name => this.routeName = name);

当你输入一个组件然后开火

this.routeNamesService.setRouteName("Settings");

(或)在app.component.ts

@ViewChild(Nav) nav: Nav;

  ngAfterViewInit() {
    this.nav.viewDidEnter.subscribe((data) => {
      console.log(data);

    });

  }

在数据中,您可以获得组件名称