在呈现“Page”之前获取数据异步 [英] Get data async before a `Page` gets rendered

查看:95
本文介绍了在呈现“Page”之前获取数据异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在呈现 Page 之前获取数据异步的正确方法是什么?

What is the correct way to get data async before a Page gets rendered?

Angular2 建议 @CanActivate decorator至我明白。遗憾的是,这不适用于Ionic2,至少不适用于我和其他人

Angular2 suggests the @CanActivate decorator as far as I understand it. Sadly this is not working with Ionic2, at least not for me and for others

显然 Ionic2 @CanActivate decorator,请参阅
但是没有记录,我无法弄清楚它究竟是做什么的。

Apparently Ionic2 does something with the @CanActivate decorator, see But its not documented and i can't figure out what it does exactly.

尽管如此这家伙指出,由于离子缓存,应该使用 Ionics View States 。他的例子如下:

Nevertheless this guy points out one should use Ionics View States instead anyways, due to ionics caching. His example looks like this:

  onPageWillEnter() { 
      return this._service.getComments().then(data => this.comments = data);
  }

看起来他期待Ionic考虑退回的承诺,但是< a href =https://github.com/driftyco/ionic-code/blob/c546dcf7dcbf334df349b45db81fbec9718d9fea/2.0.0/components/nav/view-controller.js =noreferrer>快速浏览 Ionics消息来源(至少我认为是这样)会忽略返回的值。因此,无法保证在页面呈现之前会解析承诺。以下是带有onPage *的示例,以及它如何根据需要/预期执行。

Which looks like he is expecting Ionic to consider the returned promise, but a quick glance a Ionics sources reveals (at least I think so) that the returned value is ignored. Hence there is no guarantee that the promise gets resolved before the page gets rendered. Here is an example with onPage* and how it does not perform as needed/expected.

所以我迷路了,一个人如何完成这个简单的任务?

So I'm lost, how does one achieve this simple task?

在第一个链接中,建议在导航到页面之前解析数据,这会增加被调用者页面所需数据的知识。在我看来,这不是一个选项。

In the first link, it was suggested to resolve the data before navigating to the page, which burdens the knowledge which data is needed for the page on the callee. This is not an option in my opinion.

*编辑:添加否定示例

*edit: added negative example

推荐答案

我不是确定这是否是官方方式,但我使用 Loading 组件来处理这样的情况。您可以在 Ionic API文档中找到更多信息。

I'm not sure if this is the official way to do it, but I use the Loading component for situations like this one. You can find more information in Ionic API Docs.

页面 .ts 文件如下所示:

import {Component} from '@angular/core';
import {Loading, NavController} from 'ionic-angular';

@Component({
  templateUrl:"page1.html"
})
export class Page1 {
  // Loading component
  loading : any;
  // Information to obtain from server
  comments: string = '';

  constructor(nav: NavController) {
    this.nav = nav;
  }

  onPageWillEnter() {
    // Starts the process 
    this.showLoading();
  }

  private showLoading() {
    this.loading = Loading.create({
      content: "Please wait..."
    });
    // Show the loading page
    this.nav.present(this.loading);
    // Get the Async information 
    this.getAsyncData();
  }

  private getAsyncData() {

    // this simulates an async method like
    // this._service.getComments().then((data) => { 
    //     this.comments = data);
    //     this.hideLoading();
    // });

    setTimeout(() => {
      // This would be the part of the code inside the => {...}
      this.comments = "Data retrieved from server";
      // Hide the loading page
      this.hideLoading();
    }, 5000);
  }

  private hideLoading(){
    // Hide the loading component
    this.loading.dismiss();
  }
}

代码非常简单,因此不需要进一步的细节,想法是定义一个 loading 所以我们可以显示它,然后尝试获取信息,一旦我们得到这些数据,我们就可以隐藏它调用 this.loading.dismiss()方法。

The code is very simple so it doesn't need further details, the idea is to define a loading so we can show it, then try to obtain the information, and as soon as we get that data, we can hide it calling the this.loading.dismiss() method.

你可以找到在这里使用plunker (使用beta.9)

You can find a working plunker here (using beta.9)

这篇关于在呈现“Page”之前获取数据异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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