类型“ Observable <未知[]>”不可分配给类型“ Observable <事件[]>”

时间:2020-04-15 13:45:56

标签: angular typescript firebase rxjs

尝试获取商品时出现以下错误:

Type 'Observable<unknown[]>' is not assignable to type 'Observable<Event[]>'.
Type 'unknown[]' is not assignable to type 'Event[]'.
Type '{}' is missing the following properties from type 'Event': code, name, password, pollCat

events.service.ts

import { Injectable } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument} from 'angularfire2/firestore';
import { Event } from '../models/events'
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class EventsService {

  eventsCollection : AngularFirestoreCollection<Event>;
  events : Observable<Event[]>;

  constructor(public afs: AngularFirestore) { 
    this.events = this.afs.collection('Events').valueChanges();
  }

  getEvents()
  {
    return this.events;
  }
}

events.ts

export interface Event{

    code: string;
    name: string;
    password: string;
    pollCat : string;

  }

1 个答案:

答案 0 :(得分:3)

您需要在调用<label for="myRadioId1" class="radio"> <input type="radio" name="myRadioField" id="myRadioId1" class="radio__input"> <div class="radio__radio"></div> option 1 </label><br> <label for="myRadioId2" class="radio"> <input type="radio" name="myRadioField" id="myRadioId2" class="radio__input"> <div class="radio__radio"></div> option 2 </label><br> <label for="myRadioId3" class="radio"> <input type="radio" name="myRadioField" id="myRadioId3" class="radio__input" disabled> <div class="radio__radio"></div> disabled </label> 方法时提供通用类型,而不是使用默认的value

collection

您可以在TypeScript手册here中了解有关泛型的更多信息。