Aks*_*tra 2 rxjs firebase typescript angular
当我尝试获取项目时出现以下错误:
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
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
events.ts:
export interface Event{
code: string;
name: string;
password: string;
pollCat : string;
}
Run Code Online (Sandbox Code Playgroud)
您需要在调用collection方法时提供泛型类型,而不是使用默认类型unknown:
this.events = this.afs.collection<Event>('Events').valueChanges();
// ^ here
Run Code Online (Sandbox Code Playgroud)
您可以在此处的TypeScript 手册中阅读有关泛型的更多信息。
| 归档时间: |
|
| 查看次数: |
1200 次 |
| 最近记录: |