AngularFireList'仅引用类型,但在此处用作值

时间:2018-09-07 20:53:26

标签: angular typescript ionic-framework firebase-realtime-database ionic3

我的home.ts

import { Component, ViewChild } from '@angular/core';
import { NavController, Slides } from 'ionic-angular';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import { EventDetail } from '../../models/event-detail/event-detail.interface';


@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  @ViewChild('SwipedTabsSlider') SwipedTabsSlider: Slides ;

  SwipedTabsIndicator :any= null;
  tabs:any=[];

  newEventListRef$ : AngularFireList<EventDetail>;
  newEventList$: Observable<EventDetail[]>;

  constructor(public navCtrl: NavController, private database: AngularFireDatabase) {
    this.tabs=["New", "Upcoming"];
    this.newEventListRef$ = this.database.list<EventDetail>('event-list');
    this.newEventList$ = this.newEventListRef$.valueChanges();
  }

我的home.html

<ion-content>

<ion-segment class="SwipeTabs">
<ion-segment-button *ngFor='let tab of tabs ; let i = index ' value="IngoreMe" (click)="selectTab(i)"
    [ngClass]='{ "SwipedTabs-activeTab" : ( this.SwipedTabsSlider  && ( this.SwipedTabsSlider.getActiveIndex() === i || (  tabs.length -1 === i&& this.SwipedTabsSlider.isEnd()))) }' >
        {{tab}}
    </ion-segment-button>
 </ion-segment>

  <div id='indicator' class="SwipedTabs-indicatorSegment" [ngStyle]=" 
{'width.%': (100/this.tabs.length)}"></div>

  <ion-slides #SwipedTabsSlider  (ionSlideDrag)="animateIndicator($event)"
  (ionSlideWillChange)="updateIndicatorPosition()"
  (ionSlideDidChange)="updateIndicatorPosition()"
  (pan)="updateIndicatorPosition()"
  [pager]="false">

<ion-slide>

  <ion-list>
    <ion-item *ngFor="let new of newEventList$ | async">
      <h2>{{new.eventName}}</h2>
      <h4>{{new.eventDesc}}</h4>
      <h6>{{new.lat}}</h6>
      <h6>{{new.lgt}}</h6>
    </ion-item>
  </ion-list>

</ion-slide>

<ion-slide>

</ion-slide>

错误:TypeError:Object(...)不是函数

问题尚未出现时,我似乎无法显示事件列表

TypeError:Object(...)不是函数     在SwitchMapSubscriber.project(http://localhost:8100/build/vendor.js:77814:76)     在SwitchMapSubscriber._next(http://localhost:8100/build/vendor.js:62612:27)     在SwitchMapSubscriber.Subscriber.next(http://localhost:8100/build/vendor.js:20750:18)     在RefCountSubscriber.Subscriber._next(http://localhost:8100/build/vendor.js:20786:26)     在RefCountSubscriber.Subscriber.next(http://localhost:8100/build/vendor.js:20750:18)     在Subject.next(http://localhost:8100/build/vendor.js:23237:25)     在ConnectableSubscriber.Subscriber._next(http://localhost:8100/build/vendor.js:20786:26)     在ConnectableSubscriber.Subscriber.next(http://localhost:8100/build/vendor.js:20750:18)     在Notification.observe(http://localhost:8100/build/vendor.js:52585:50)     在AsyncAction.DelaySubscriber.dispatch(http://localhost:8100/build/vendor.js:80114:40

1 个答案:

答案 0 :(得分:1)

有几个问题。尝试将类型设置为=时,最主要的是使用:而不是newEventListRef$AngularFireList<EventDetail>。下一个问题是()之后的括号AngularFireList<EventDetail>。删除括号()并将=替换为:应该可以解决此问题。我还建议像db.list()一样向this.db.list<EventDetail>('event-detail')提供type

newEventListRef$: AngularFireList<EventDetail>;

用法示例:

import { AngularFireDatabase, AngularFireList } from '@angular/fire/database';
import { Observable } from 'rxjs';

@Component({
  template: `
    <ul>
      <li *ngFor="let item of newEventList$ | async">{{item.someProperty}}</li>
    </ul>
  `
})
export class HomePage {
  newEventListRef$: AngularFireList<EventDetail>;
  newEventList$: Observable<EventDetail[]>;

  constructor(private db: AngularFireDatabase) {
    this.newEventListRef$ = this.db.list<EventDetail>('event-detail');
    this.newEventList$ = this.newEventListRef$.valueChanges();
  }
}

希望有帮助!