Ionic VirtualScroll无法读取null的属性“length” [英] Ionic VirtualScroll Cannot read property 'length' of null

查看:295
本文介绍了Ionic VirtualScroll无法读取null的属性“length”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Ionic 3中使用虚拟滚动但它无效。

I´m trying to use virtual scroll in Ionic 3 but it is not working.

我在我的提供商处有这个功能:

I have this function on my provider:

getActiveAds(){
    return this.afDb.list<AngularFireList<any>>('/ads-active', ref => ref.orderByChild('adPlanPriority').startAt(1).endAt(3))
  }

在我的列表页面上,我有这个:

On my list page, i have this:

constructor(public navCtrl: NavController, public navParams: NavParams, public loadingCtrl: LoadingController, public adProvider: AdProvider) {
    this.loading = this.loadingCtrl.create();
    this.loading.present();

    this.ads = this.adProvider.getActiveAds().valueChanges()
    this.ads.subscribe((cat)=> {
      this.loading.dismiss()
    })
  }

和我的list.html:

and my list.html this:

<ion-list no-lines [virtualScroll]="ads | async">
        <button ion-item *virtualItem="let ad" (click)="onAdSelect(ad)" class="aero-item ">
            <ion-thumbnail item-start>
                <img src="assets/images/noimage.jpg" />
            </ion-thumbnail>
            <h2>{{ ad.model}}</h2>

        </button>
    </ion-list>

使用此代码,我收到此错误:

With this code, I´m getting this error:

Cannot read property 'length' of null
TypeError: Cannot read property 'length' of null
    at VirtualScroll._stepDOMWrite (http://localhost:8100/build/vendor.js:92118:60)
    at http://localhost:8100/build/vendor.js:92078:23
    at dispatch (http://localhost:8100/build/vendor.js:20601:9)
    at DomController._flush (http://localhost:8100/build/vendor.js:20545:13)
    at rafCallback (http://localhost:8100/build/vendor.js:20534:22)

我做错了什么?

推荐答案

我遇到了同样的问题并找到了解决方法,虽然它不是最佳的。

I got the same issue and found a workaround, although it's not optimal.

您可以从文档中看到 [virtualScroll] 期待数组。当您的 this.ads 返回可观察时。这就是为什么你得到无法读取null 的属性'length'。

You see from the documentation [virtualScroll]expect an Array. While your this.ads return an Observable. That's why you got Cannot read property 'length' of null.

解决方法:首先订阅它然后显示在查看(没有异步管道),完成后也要记住unsubsribe。

Workaround: Subscribe to it first then display on view (without async pipe), also keep in mind to unsubsribe when done.

ads: any[] = [];
constructor(public adProvider: AdProvider) {
    this.adProvider.getActiveAds().valueChanges().subscribe(results => this.ads = results);  
  }

这篇关于Ionic VirtualScroll无法读取null的属性“length”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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