角度2(Ionic 2)在页面显示时调用页面中的功能 [英] Angular 2 (Ionic 2) call a function in a page when ever page is displayed

查看:157
本文介绍了角度2(Ionic 2)在页面显示时调用页面中的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的主页在 角色2(离子2) 应用程序加载时,我想要呼叫服务/功能。如何实现这一点?

When ever my home page in angular 2(ionic 2) app is loaded I want call service/function. How to achieve this?

首次加载应用程序(主页加载)时,我可以在 构造函数 ,但是当用户开始使用该应用程序并且 新页面 导航控制器 pop 返回到 主页 构造函数 不会再调用。

For the first time when the app is loaded(home page is loaded) I can write this in the constructor, but when user start using the app and push new pages into nav controller and pop them to come back to home page, the constructor wont get called again.

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

I would like to know what is the best way to achieve this feature?

我是新的 angular2 ion2 框架 (也没有 角度1 ion1 ),请帮助我。

I am new to angular2 and ionic2 framework (also don't have experiences in angular1 and ionic1), please help me out.

很多谢谢。

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


推荐答案

strong> onPageWillEnter() 为我工作。

onPageWillEnter() worked for me.

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

离子生命周期钩

这篇关于角度2(Ionic 2)在页面显示时调用页面中的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆