您正在键入返回类型为Observable<Product[]> 的函数,
但您的方法中没有 return 关键字。
要修复它,只需返回 Observable<T>:
getAllProducts(id, token): Observable<Product[]> {
this.model = 'product';
return this.http.get(this.getUrlById(id), {headers: {'Authorization' : `Bearer ${token}`}})
}
或者将返回值键入为Subscription:
getAllProducts(id, token): Subscription {
this.model = 'product';
return this.http.get(this.getUrlById(id), {headers: {'Authorization' : `Bearer ${token}`}})
.pipe(map((products:Product[] => products))
.subscribe((products) => this.getAllProdObserv.next(products))
}
从最后一个例子中,我想你也可以跳过pipe + map。这只是一个额外的步骤,在这种情况下什么都不做:
.pipe(map((products:Product[] => products))
我只想删除那行:
getAllProducts(id, token): Subscription {
this.model = 'product';
return this.http.get(this.getUrlById(id), {headers: {'Authorization' : `Bearer ${token}`}})
.subscribe((products) => this.getAllProdObserv.next(products))
}
一般来说,我会返回Observable<T>(如第一个示例),而不是返回订阅。