在呈现Page之前获得数据异步的正确方法是什么?
据我所知,Angular2建议@CanActivate装饰师。遗憾的是,这并不适用于Ionic2,至少对我和其他来说都是如此。
显然,Ionic2对@CanActivate装饰师做了些什么,看见,但是它没有被记录下来,我也搞不清楚它到底做了什么。
尽管如此,这家伙指出,无论如何都应该使用Ionics View States作为替代,这是因为采用了仿生学缓存。他的例子如下:
onPageWillEnter() {
return this._service.getComments().then(data => this.comments = data);
}这看起来像是他期待Ionic会考虑返回的承诺,但是一个快速扫视 --一个Ionics -显示(至少我认为是这样)返回的值被忽略了。因此,不能保证承诺在呈现页面之前得到解决。下面是一个带有onPage*的示例,以及它如何不按需要/预期执行。
所以我迷路了,一个人是如何完成这个简单的任务的?
在第一个链接中,建议在导航到页面之前解析数据,这将增加被调用页面上页面所需数据的知识。在我看来,这不是一个选择。
*编辑:添加负面示例
发布于 2016-10-23 17:04:53
对于任何在使用Ionic 2时限制页面访问的爬行堆栈溢出的人来说,Ionic的推荐生命周期事件应该是ionViewCanEnter。
文档中的:
ionViewCanEnter在视图进入之前运行。在经过身份验证的视图中,您需要在视图进入之前检查权限,这可以用作一种“保护”。
http://ionicframework.com/docs/v2/api/navigation/NavController/
发布于 2016-06-24 13:08:58
我不确定这是否是官方的方法,但在这种情况下,我使用Loading组件。您可以在离子API文档中找到更多信息。
页面.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()方法来隐藏它。
您可以找到一个工作柱塞 (使用beta.9)
发布于 2016-05-19 11:47:46
如果您只从一个位置导航到该页面,那么就不能在导航和使用NavParams传递数据之前简单地加载数据吗?
我在配置文件页面中使用此结构,从某个索引页面中单击用户,然后在访问他的配置文件之前检索他的配置文件。在这种情况下,您可以展示一个不错的Loading。
let self=this;
this.profileSvc.getProfile(id)
.then(profile => {
self.nav.push(Profile, {profile:profile});
});现在,在配置文件页中,您可以使用NavParams初始化页面。
export class Profile {
profile:any;
constructor(private params:NavParams) {
if(params.get('profile')) {
this.profile = params.get('profile');
} else {
this.profile = this.me;
}
}https://stackoverflow.com/questions/36569179
复制相似问题