Angular 2 (Ionic 2) 在页面显示时调用页面中的函数

Angular 2 (Ionic 2) call a function in a page when ever page is displayed

当我在 angular 2(ionic 2) 应用程序中的主页加载时,我想调用 service/function。如何实现?

第一次加载应用程序(加载主页)时,我可以在 constructor 中写这个,但是当用户开始使用该应用程序并且push 新页面分为 nav controllerpop 它们回到 home pageconstructor 将不会再被调用。

我卡在这里了。

我想知道实现此功能的最佳方法是什么?

我是 angular2ionic2 framework 的新手(也没有angular1ionic1的经验,请帮帮我.

非常感谢。

更新

我尝试过但没有成功的示例代码。

import {Page, NavController, Platform, Storage, SqlStorage} from 'ionic-angular';

@Page({
  templateUrl: 'build/pages/page1/page1.html'
})
export class Page1 {
    static get parameters(){
        return [[NavController],[Platform]];
    }
    ngOnInit() {
        console.log("Showing the first page!");
    }
    constructor(nav, platform){
        this.nav = nav;
        this.platform =  platform;
    }
}

您可以利用 lifeCycle-hooks,特别是 ngOnInit()ngAfterViewInit()

Here是一个简单的教程。

例如:

// Annotation section
@Component({
  selector: 'street-map',
  template: '<map-window></map-window><map-controls></map-controls>',
})
// Component controller
class StreetMap {
  ngOnInit() {  //here you can call the function wanted
    // Properties are resolved and things like
    // this.mapWindow and this.mapControls
    // had a chance to resolve from the
    // two child components <map-window> and <map-controls>
  }
}

更新: 它适用于纯 Angular2 应用程序,IonicFramework 具体解决方案参见

@DeepakChandranP 的 回答。

onPageWillEnter() 为我工作。

import {Page, NavController, Platform, Storage, SqlStorage} from 'ionic-angular';


@Page({
  templateUrl: 'build/pages/page1/page1.html'
})
export class Page1 {
    static get parameters(){
        return [[NavController],[Platform]];
    }
    onPageWillEnter() {
        console.log("Showing the first page!");
    }
    constructor(nav, platform){
        this.nav = nav;
        this.platform =  platform;
    }
}

Ionic lifecycle hook

IONIC 2 RC1更新

ionViewWillEnter() {
    console.log("this function will be called every time you enter the view");
}