首页 > 解决方案 > 使用 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.

标签: angular

解决方案


尝试这个:

      getAllProducts( numberOfResults = 10) {
  return this.http.get<any>(this.SERVER_URL + '/products', {
    params: {
      limit: numberOfResults.toString()
    }
  });

推荐阅读