在渲染“页面"之前获取数据异步

Get data async before a `Page` gets rendered(在渲染“页面之前获取数据异步)
本文介绍了在渲染“页面"之前获取数据异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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

Angular2 建议使用 @CanActivate 装饰器.可悲的是,这不适用于 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 装饰器 见但它没有记录,我无法弄清楚它到底做了什么.

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

尽管如此 这家伙指出应该使用 无论如何,由于离子缓存,离子视图状态.他的例子是这样的:

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 考虑返回的承诺,但是 快速浏览 离子源显示(至少我是这么认为的)返回值被忽略.因此,不能保证在页面呈现之前 会解决承诺.这是一个带有 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.

*添加反例

推荐答案

对于在使用 Ionic 2 时对 Stackoverflow 进行限制页面访问的任何人来说,Ionic 推荐的生命周期事件似乎是 ionViewCanEnter.

For anyone crawling Stackoverflow about restricting page access when using Ionic 2, it looks like Ionic's recommended lifecycle event to tap into is ionViewCanEnter.

来自文档:

ionViewCanEnter 在视图进入之前运行.这可以用作经过身份验证的视图中的一种守卫",您需要在视图进入之前检查权限.

ionViewCanEnter Runs before the view can enter. This can be used as a sort of "guard" in authenticated views where you need to check permissions before the view can enter.

http://ionicframework.com/docs/v2/api/navigation/NavController/

这篇关于在渲染“页面"之前获取数据异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)