Ionic 2页面更改订阅

3

我如何在Ionic 2中订阅页面更改?我只想全局记录页面名称/标题。是否有可以订阅的事件?


你可以使用事件来实现类似的功能。 - Suraj Rao
我想要全局地做到这一点,这样我就不必触碰每个文件。 - mark
你应该监听应用程序组件根NavController的事件。 - JoeriShoeby
1
@JoeriShoeby 如果您能将其转化为一个带有一些示例代码的答案,我将非常乐意接受它。 - mark
1个回答

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);

    });

  }

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接