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

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

问题描述

当我在 angular 2(ionic 2) 应用程序中的主页加载时,我想调用服务/功能.如何做到这一点?

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

第一次加载应用程序时(加载主页)我可以在 constructor 中写这个,但是当用户开始使用应用程序和 push 新页面到 nav controllerpop 到回到主页constructor不会再被调用.

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?

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

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

推荐答案

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

离子生命周期挂钩

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

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