gpt4 book ai didi

angular - 我收到错误 : Type 'Subscription' is not assignable to type 'Observable'

转载 作者:行者123 更新时间:2023-12-04 01:35:26 24 4
gpt4 key购买 nike

在这里,我的服务中有两种方法。我对此很陌生,所以我不确定 subscribe 方法的作用,但我所知道的是,没有它我无法构建我的项目。如果我评论这两个函数,然后 ng serve,然后取消注释,让它刷新更改,它就可以工作。但我无法构建项目。如何更改代码,以免出现这些错误并成功构建项目?

public delete(patient: Patient): Observable<Patient> {
return this.http.delete<Patient>(`${this.deleteOneUrl}/${patient.id}`).subscribe();
}

public addPatient(patient: Patient): Observable<Patient> {
return this.http.post<Patient>(this.addOneUrl, patient).subscribe();
}

ERROR in src/app/patient.service.ts(30,3): error TS2322: Type 'Subscription' is not assignable to type 'Observable'. Property '_isScalar' is missing in type 'Subscription'. src/app/patient.service.ts(38,5): error TS2322: Type 'Subscription' is not assignable to type 'Observable'.

最佳答案

调用 subscribe() , 你收到 Subscription对象而不是 Observable (您只需调用 delete/post 方法即可获得)。

要么将返回类型更改为 Subscription ,或返回 Observable (删除 .subscribe() )。

通常,您应该从您的服务返回可观察对象并稍后订阅(在某些点击处理程序中,甚至在模板中)。通过在服务中订阅,你会立即触发http调用,而返回observable是惰性的,稍后订阅时会触发http调用)。

关于angular - 我收到错误 : Type 'Subscription' is not assignable to type 'Observable' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50132214/

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