gpt4 book ai didi

angular - 类型 'Observable' 不可分配给类型 'Observable'

转载 作者:行者123 更新时间:2023-12-02 02:55:25 24 4
gpt4 key购买 nike

当我尝试获取项目时收到以下错误:

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;

}

最佳答案

调用collection方法时,您需要提供泛型类型,而不是使用默认的unknown:

this.events = this.afs.collection<Event>('Events').valueChanges();
// ^ here

您可以在 TypeScript 手册 here 中阅读有关泛型的更多信息。 .

关于angular - 类型 'Observable<unknown[]>' 不可分配给类型 'Observable<Event[]>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61230347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com