【问题标题】:Ionic VirtualScroll Cannot read property 'length' of nullIonic VirtualScroll 无法读取 null 的属性“长度”
【发布时间】:2018-03-27 04:25:08
【问题描述】:

我正在尝试在 Ionic 3 中使用虚拟滚动,但它不起作用。

我的提供商有这个功能:

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

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

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 这个:

<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>

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

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)

我做错了什么?

【问题讨论】:

  • 我不确定 virtualScroll 是否可以与异步一起使用。尝试用ngFor替换,看看结果

标签: ionic3


【解决方案1】:

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

你从the documentation[virtualScroll]看到一个数组。而你的this.ads 返回一个Observable。这就是你得到Cannot read property 'length' of null的原因。

解决方法:先订阅它然后显示在视图上(没有异步管道),还请记住在完成后取消订阅。

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

【讨论】:

  • 我照你说的做,但现在,我得到一个不同的错误:“无法读取属性'dispose' of null
  • 也许你可以在link 上查看我的这个测试项目。请注意我在 Observable 上应用 virtualscroll 的 speaker page。希望这可以帮助:)
猜你喜欢
  • 1970-01-01
  • 2023-03-11
  • 2013-03-21
  • 2015-04-06
  • 2019-08-05
  • 1970-01-01
  • 1970-01-01
  • 2018-03-27
  • 1970-01-01
相关资源
最近更新 更多