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

时间:2023-03-16
本文介绍了在渲染“页面"之前获取数据异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在渲染 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/

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

上一篇:卡在按值/子键删除父推送键 下一篇:如何在 Ionic 2 中定位弹出框

相关文章