angular - 使用 Angular 进行 Web 开发,错误“没有重载匹配此调用”
问题描述
执行此代码:
import { Component, OnInit } from '@angular/core';
import { ProductService } from 'src/app/service/product.service';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
products: any[] = [];
constructor(private productService: ProductService) { }
ngOnInit(): void {
this.productService.getAllProducts().subscribe((prods: {count: Number, products: any[]}) => {
this.products = prods.products;
});
}
}
我收到此错误:
Error: src/app/component/home/home.component.ts:16:54 - error TS2769: No overload matches this call.
Overload 1 of 5, '(observer?: NextObserver<Object> | ErrorObserver<Object> | CompletionObserver<Object> | undefined): Subscription', gave the following error.
Argument of type '(prods: { count: Number; products: any[];}) => void' is not assignable to parameter of type 'NextObserver<Object> | ErrorObserver<Object> | CompletionObserver<Object> | undefined'.
Property 'complete' is missing in type '(prods: { count: Number; products: any[];}) => void' but required in type 'CompletionObserver<Object>'.
Overload 2 of 5, '(next?: ((value: Object) => void) | undefined, error?: ((error: any) => void) | undefined, complete?: (() => void) | undefined): Subscription', gave the following error.
Argument of type '(prods: { count: Number; products: any[];}) => void' is not assignable to parameter of type '(value: Object) => void'.
Types of parameters 'prods' and 'value' are incompatible.
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
Type 'Object' is missing the following properties from type '{ count: Number; products: any[]; }': count, products
16 this.productService.getAllProducts().subscribe((prods: {count: Number, products: any[]}) => {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/rxjs/internal/types.d.ts:64:5
64 complete: () => void;
~~~~~~~~
'complete' is declared here.
解决方案
尝试这个:
getAllProducts( numberOfResults = 10) {
return this.http.get<any>(this.SERVER_URL + '/products', {
params: {
limit: numberOfResults.toString()
}
});
推荐阅读
- typescript - Vue 3 TypeScript - 模块“XX”在本地声明“YY”,但未导出
- python - Python Flask - 想要显示“Hello [Firstname]
- android - 如何循环遍历要写入本地存储的文件列表?
- python - Pycharm 中 Colab 到 Pycharm 库的导入问题
- javascript - NodeJS Express API 调用结构异步事件
- flutter - 如何限制我的 ListView.builder 中的项目数?
- python - python为什么要重用默认值字典?
- php - 如何在 PHP 中反转此类代码
- python - 如何更快地加载 api 响应并在 pandas 中使用它在 django 中进行分析?
- apache-karaf - Opendaylight Karaf 在安装引导功能时出错