【问题标题】:Ionic2 "ionViewDidEnter" method is very slow first timeIonic2“ionViewDidEnter”方法第一次很慢
【发布时间】:2017-07-23 04:22:35
【问题描述】:

你能告诉我为什么下面的功能在首次加载时会有很大的延迟吗?第二次正常。实际设备上也有同样的延迟。请看附件视频。

注意: 这里的问题是 firebase bind this.eventData.getEventList().on('value', snapshot => {} 当我删除它时一切正常。你知道怎么做吗?

视频: Video about this issue

.html

<ion-card *ngFor="let event of eventList" (click)="goToEventDetail(event.id)" color="secondary">
    <ion-card-content>
      <p>Event Name: <strong>{{event?.name}}</strong></p>
      <p>Ticket: <strong>${{event?.price}}</strong></p>
      <p>Cost: <strong>${{event?.cost}}</strong></p>
      <p>Date: <strong>{{event?.date}}</strong></p>
    </ion-card-content>
</ion-card>

.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { EventDetailPage } from '../event-detail/event-detail';
import { EventData } from '../../providers/event-data';

@Component({
  selector: 'page-event-list',
  templateUrl: 'event-list.html',
})

export class EventListPage {
  public eventList: any;
  constructor(public navCtrl: NavController, public eventData: EventData) {

  }

  ionViewDidEnter() {
    this.eventData.getEventList().on('value', snapshot => {
      let rawList = [];
      snapshot.forEach(snap => {
        rawList.push({
          id: snap.key,
          name: snap.val().name,
          price: snap.val().price,
          cost: snap.val().cost,
          date: snap.val().date
        });
      });
      this.eventList = rawList;
    });
  }
}

provider.ts

import { Injectable } from '@angular/core';
import firebase from 'firebase';

@Injectable()
export class EventData {

  public currentUser: any;
  public eventList: any;

  constructor() {
    this.currentUser = firebase.auth().currentUser.uid;
    this.eventList = firebase.database().ref('userProfile/' + this.currentUser + '/eventList');

  }

  getEventList(): any {
    return this.eventList;
  }

}

【问题讨论】:

  • 签入邮递员,您可以看到 api 调用所用的持续时间,如果与此相同,那么后端应该快速发送响应

标签: angular firebase ionic-framework ionic2


【解决方案1】:

似乎没有检测到更改。尝试将您的 push 包装成 NgZone::run()

首先,导入 NgZone:

import { NgZone } from '@angular/core';

然后,注入你的构造函数:

constructor(public zone: NgZone /* ... other injections ... */) {
    // constructor code ...
}

然后使用run函数将数据压入数组:

snapshot.forEach(snap => {
    this.zone.run(() => {
        rawList.push({
            id: snap.key,
            name: snap.val().name,
            price: snap.val().price,
            cost: snap.val().cost,
           date: snap.val().date
        });
    });
});

我不确定这是否适合你,但值得一试。

【讨论】:

  • sorry.no difference.same delay there too。这是这个firebase绑定this.eventData.getEventList().on('value', snapshot =&gt; {的问题吗?
  • 我不这么认为...这看起来不错..您可以在回调中放置一个控制台日志以查看它是立即被调用还是仅在视图更新时才被调用?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-27
  • 2017-05-23
  • 1970-01-01
相关资源
最近更新 更多