ionic3——判断当所在前页面(app.component.ts or provider)

在app.component.ts中判断是否在某个页面,从而执行不同操作:

import {ViewChild} from '@angular/core';
import {HomePage} from '../pages/home/home';
.
.
@ViewChild(Nav) nav: Nav;


let activeVC = this.nav.getActive();
let page = activeVC.instance;
//this.navCtrl.getActive().name == 'HomePage'   也可以
if (page instanceof HomePage) {
}

然后,如果这种判断情况出现过多,我想在provider中封装个方法呢:

import {App } from 'ionic-angular';
constructor(private app: App){}
/**
   * 是否在某个页面
   * @param page like->import {HomePage} from '../pages/home/home';
   * @return {boolean}
   */
  inPageing(page): boolean {
    //注意:(getActiveNav) is deprecated and will be removed in the next major release. Use getActiveNavs instead.
    let activeVC = this.app.getActiveNavs()[0].getActive();
    let newPage = activeVC.instance;
    if (newPage instanceof page) {//当前页面对讲是否是页面
      return true;
    }else{
      return false;
    }
  }
``

你可能感兴趣的:(ionic3——判断当所在前页面(app.component.ts or provider))