离子页面没有缓存 [英] Ionic pages not cached

查看:184
本文介绍了离子页面没有缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两页:


  • 主页

  • 关于页面

HomePage是 rootPage

HomePage is the rootPage.

启动时调用主页#ionViewDidLoad 。我使用NavController从HomePage导航到AboutPage:

At startup HomePage#ionViewDidLoad is called. I navigate from the HomePage to the AboutPage using the NavController:

navigateToAbout(): void {
   this.navCtrl.push('AboutPage');
}

每次我导航到AboutPage, AboutPage#ionViewDidLoad 被调用。如果我使用 ion-navbar 导航回主页, HomePage#ionViewDidLoad 如果我使用 navCtrl.push('HomePage'),则再次调用 HomePage#ionViewDidLoad

Everytime I navigate to the AboutPage, AboutPage#ionViewDidLoad is called. If I navigate back to the HomePage with ion-navbar, HomePage#ionViewDidLoad is not called but if I use navCtrl.push('HomePage'), HomePage#ionViewDidLoad is called again.

有人可以解释,为什么每次调用ionViewDidLoad如果我使用 navCtrl.push(...)。根据 Ionic NavController Doc ,应该缓存页面并且只应该调用ionViewDidLoad每页一次:

Can someone explain, why ionViewDidLoad is called everytime if I use navCtrl.push(...). According to the Ionic NavController Doc the Pages should be cached and ionViewDidLoad should be called only once per Page:


创建视图

默认情况下,页面被缓存并留在DOM中,如果它们被导航
,但仍然在导航堆栈中(例如,
push()上的退出页面)。从
导航堆栈(pop()或setRoot())中删除它们时会被销毁。

By default, pages are cached and left in the DOM if they are navigated away from but still in the navigation stack (the exiting page on a push() for example). They are destroyed when removed from the navigation stack (on pop() or setRoot()).

ionViewDidLoad

页面加载后运行。每个页面创建时,此事件仅发生
。如果页面已离开但已缓存,则此
事件将不会在后续查看时再次触发。 ionViewDidLoad
事件是放置页面设置代码的好地方。

Runs when the page has loaded. This event only happens once per page being created. If a page leaves but is cached, then this event will not fire again on a subsequent viewing. The ionViewDidLoad event is good place to put your setup code for the page.


推荐答案

因为如果您使用导航栏导航回HomePage,则使用 pop()方法。如果你在AboutPage上使用 push('HomePage'),你创建一个新的HomePage实例,然后 ionViewDidLoad()被调用。

Because if you use the navbar to navigate back to the HomePage the pop() method is used. If you use push('HomePage') on the AboutPage you create a new instance of the HomePage and subsequently ionViewDidLoad() is invoked.

只有已经在导航堆栈上的页面被缓存(如第一次推送AboutPage时的HomePage),但推送到导航堆栈的页面总是新创建。

Only pages that are already on the navigation stack are cached (like the HomePage when you push the AboutPage the first time) but pages that are pushed to the nav stack are always newly created.

也许这个例子有助于形象化:

1。启动后的初始状态:

[HomePage]

2。在$ code>之后的状态nav.push('AboutPage'):

[HomePage, AboutPage]
 ^^^^^^^^
  cached

3。说明如果您使用导航栏导航回来或 pop()

[HomePage] // The cached instance is used so ionViewDidLoad() is not called

4。如果您在第二步之后使用 .push('HomePage'),请说明:

4. State if you use .push('HomePage') after the second step:

[HomePage, AboutPage, HomePage] // a new instance is created so ionViewDidLoad() is called
 ^^^^^^^^  ^^^^^^^^^
  cached    cached

这篇关于离子页面没有缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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