简体   繁体   English

Ionic 3:如何预加载标签?

[英]Ionic 3 : How to preload tabs?

I'm in a particular case where I absolutely need to make sure that document.querySelectorAll('.scroll-content')[0] , document.querySelectorAll('.scroll-content')[1] , etc are defined before the user enters the app. 在特定情况下,我绝对需要确保在定义了document.querySelectorAll('.scroll-content')[0]document.querySelectorAll('.scroll-content')[1]等之前用户进入应用程序。 In other words : I need to preload my tabs . 换句话说: 我需要预加载我的标签

The current behavior of tabs : 选项卡的当前行为:

  • doc...All('.scroll-content')[0] is defined. doc...All('.scroll-content')[0]已定义。
  • doc...All('.scroll-content')[1] is undefined. doc...All('.scroll-content')[1]未定义。
  • doc...All('.scroll-content')[2] is undefined. doc...All('.scroll-content')[2]未定义。
  • doc...All('.scroll-content')[3] is undefined. doc...All('.scroll-content')[3]未定义。

The reason for this "state" is that the content of the page of a tab is loaded only when it is visited. 此“状态”的原因是,仅在访问选项卡页面的内容时才对其进行加载。

The behavior I expect from the tabs 我期望标签中的行为

  • doc...All('.scroll-content')[0] is defined. doc...All('.scroll-content')[0]已定义。
  • doc...All('.scroll-content')[1] is defined. doc...All('.scroll-content')[1]已定义。
  • doc...All('.scroll-content')[2] is defined. doc...All('.scroll-content')[2]已定义。
  • doc...All('.scroll-content')[3] is defined. doc...All('.scroll-content')[3]已定义。

So I need to be able to load the tabs pages before the user is visiting them for the first time. 因此,我需要能够在用户首次访问之前加载标签页。

Sadly the ionic team did remove the preloading of tabs in beta 12 because supposedly the AOT compilation is fast enough for us "not to need" to preload tabs. 可悲的是,离子团队确实删除了beta 12中的选项卡预加载,因为据认为AOT编译足够快,我们“不需要”预加载选项卡。 😤 😤

I also tried to set my @IonicPages priority to high and specifying that I want to preload modules in app.module.ts. 我还尝试将@IonicPages优先级设置为高,并指定要在app.module.ts中预加载模块。

No success. 没有成功

Any idea ? 任何想法 ?

PS : Please, no ugly stuff like programmatically selecting each tab before showing content. PS:请不要出现丑陋的东西,例如在显示内容之前以编程方式选择每个选项卡。 🤮 🤮

EDIT : I'm still looking for a clean workaround, but I also posted a feature request on github . 编辑:我仍在寻找一种干净的解决方法,但我还在github上发布了功能要求 Feel free to participate. 随时参加。

Note that I speak from my own experience. 请注意,我是根据自己的经验讲的。

Background: I came across very similar approach where I render some content on the page with some external engine and need to make a reference to HTML generated by it within an Angular component. 背景:我遇到了非常相似的方法,我使用一些外部引擎在页面上呈现了一些内容,并且需要在Angular组件中引用由它生成的HTML。 The problem is that putting the code in after view init lifecycle function wasn't enough. 问题在于,将代码放入after view init生命周期函数还不够。

Possible Solutions: 可能的解决方案:

  1. You could use setTimeout to wait for the HTML to be rendered 您可以使用setTimeout等待HTML呈现
  2. You can tap into ngDoCheck and check if the HTML has been rendered (if document.querySelector is truthy then you know you can move to your script). 您可以点击ngDoCheck并检查HTML是否已呈现(如果document.querySelector是真实的,那么您就可以移动到脚本了)。
  3. Tapping to `ngDoCheck is expensive (after your initial load, your checks will still be fired). 轻触`ngDoCheck是很昂贵的(在您的初始加载后,您的检查仍将被触发)。 Hence it is better to use a combination of while loop and setTimeout. 因此,最好同时使用while循环和setTimeout。 See pseudo-code below: 请参见下面的伪代码:

- --

ngOnViewInit() {
  let isReady = false;
  do {
    if (document.querySelectorAll('.scroll-content').length > 0) {
      isReady = true;
    }
  while (!isReady);
  this.runMyCodeThatNeedsThePagePreloaded();
}

If you are using AOT and the ionic team is right (it is fast enough), I could see no reason why you would want to preload your tabs. 如果您使用的是AOT,而离子团队是对的(速度足够快),我看不出您为什么要预加载标签页。 Except maybe because you want to fetch some data? 也许是因为您要获取一些数据?

If loading data is the case, you could fetch the data in something a tabService which you could inject in each tab. 如果是加载数据,则可以在可以插入每个选项卡的tabService中获取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM