Angular 运算符函数<;未知,未知>;类型';中缺少以下属性;可观察的';:_isScalar,来源,操作员,
在Angular中为HTTP请求实现getProducts()方法时遇到问题。我将observable设置为匹配我的接口Angular 运算符函数<;未知,未知>;类型';中缺少以下属性;可观察的';:_isScalar,来源,操作员,,angular,typescript,observable,Angular,Typescript,Observable,在Angular中为HTTP请求实现getProducts()方法时遇到问题。我将observable设置为匹配我的接口observable,以从productUrL获取并返回JSON文件中的产品,但它会抛出一个错误: Type 'OperatorFunction<unknown, unknown>' is missing the following properties from type 'Observable<IProduct[]>': _isScalar, so
observable
,以从productUrL
获取并返回JSON文件中的产品,但它会抛出一个错误:
Type 'OperatorFunction<unknown, unknown>' is missing the following properties from
type 'Observable<IProduct[]>': _isScalar, source, operator, lift, and 6 more.
下面是produst list.component
,它正在调用方法onInit
errorMessage: string;
products: IProduct[] = [];
ngOnInit() {
this.productService.getProducts().subscribe({
next: products => this.products = products,
error: err => this.errorMessage = err
});
您得到的错误很可能是因为这一行:
tap(data => console.log("All: " + JSON.stringify(data))))
您可以使用箭头函数(或匿名函数),但必须记住,如果您没有将代码放入括号{}
,则表示您正在返回此代码的结果。
这意味着,您的代码与以下代码相同:
tap(data => {
return console.log("All: " + JSON.stringify(data)))
}
);
我也在谈论这一点:
next: products => this.products = products,
error: err => this.errorMessage = err
试试这个,乍一看,catchError应该在管道内
next: products => this.products = products,
error: err => this.errorMessage = err
.pipe(
tap(data => console.log("All: " + JSON.stringify(data))),
catchError(this.handleError)
)