Angular 2(Ionic 2)在显示页面时调用页面中的函数 [英] Angular 2 (Ionic 2) call a function in a page when ever page is displayed
问题描述
当我在 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 controller
和 pop
到回到主页
,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?
我是 angular2
和 ionic2
framework
(也没有angular1
和ionic1
的经验),请帮帮我.
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屋!