“没有过载匹配此调用";在Angular 11中 [英] "No overload matches this call" in Angular 11

查看:64
本文介绍了“没有过载匹配此调用";在Angular 11中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 home.component.ts 文件中遇到以下错误时,我正在做一个Angular教程:

I was doing an Angular tutorial, when I ran into the following error in the home.component.ts file:

没有重载匹配此调用.5的重载1'(观察者?:PartialObserver< HttpResponse>):订阅',给出了以下错误.类型'(data:any [])=>的参数无效"不能分配给"PartialObserver< HttpResponse>"类型的参数.类型'(data:any [])=>中缺少属性'complete'.无效",但在"CompletionObserver< HttpResponse>"类型中是必需的.重载2之5,((next ?:(value:HttpResponse)=> void,error ?:(error:any)=> void,complete ?:()=> void):订阅'',给出以下内容错误.类型'(data:any [])=>的参数无效"不能分配给类型为'(value:HttpResponse)=>的参数.空白'.参数数据"和值"的类型不兼容.类型"HttpResponse"缺少类型"any []"中的以下属性:length,pop,push,concat和另外26个.

No overload matches this call. Overload 1 of 5, '(observer?: PartialObserver<HttpResponse>): Subscription', gave the following error. Argument of type '(data: any[]) => void' is not assignable to parameter of type 'PartialObserver<HttpResponse>'. Property 'complete' is missing in type '(data: any[]) => void' but required in type 'CompletionObserver<HttpResponse>'. Overload 2 of 5, '(next?: (value: HttpResponse) => void, error?: (error: any) => void, complete?: () => void): Subscription', gave the following error. Argument of type '(data: any[]) => void' is not assignable to parameter of type '(value: HttpResponse) => void'. Types of parameters 'data' and 'value' are incompatible. Type 'HttpResponse' is missing the following properties from type 'any[]': length, pop, push, concat, and 26 more.

我已经查找过,但是无法找到解决此问题的任何方法.我试图在 tsconfig.json 中禁用 strict noImplicitAny noImplicitReturns ,但是它们尚未解决这个问题.我希望有一个解决方案,同时将所有三个设置都保留在 tsconfig.json 文件中的 true 上.

I have looked it up, but have been unable to find any solutions to this issue. I have tried to to disable strict, noImplicitAny, noImplicitReturns in the tsconfig.json, but they have not solved this issue. I would prefer if there was a solution to this issue while keeping all three settings on true in the tsconfig.json file.

任何帮助将不胜感激.

谢谢

编辑:在处理AJAX调用后的数据时,在 home.component.ts 文件中发生错误:

The error occurs in the home.component.ts file when dealing with the data after the AJAX call:

this.dataService.sendGetRequest().subscribe(data: any[] => {
      this.products = data;
    });
  }

推荐答案

首先:如果您将相关代码放在问题本身中,而不仅仅是到stackblitz的链接,这将是一种礼貌.错误消息给您的第一件事是:哪个文件,哪个行,哪个位置(src/app/home/home.component.ts中的 Error(19:49)).哪个是

First: it would be a courtesy if you put your relevant code in the question itself, instead of just the link to stackblitz. The first thing the error message gives you is: which file, which line, which position (Error in src/app/home/home.component.ts (19:49)). Which is

    this.dataService.sendGetRequest().subscribe((data: any[]) => {
//                                              ^ about here

第二:您在HttpClient调用中放入 observe:"response" .好吧,在这种情况下,您不会在响应对象中得到简单的 data:any [] ,而是整个 HttpResponse 对象.您需要访问其 body 属性.我建议添加

Second: you put observe: "response" in your HttpClient call. Well, in such case you won't get simply data: any[] in your response object, but a whole HttpResponse object. You need to access its body property. I suggest adding

map({ body } => body)

位于 DataService 中的 sendGetRequest 中管道的末尾.

at the end of the pipe in sendGetRequest in DataService.

这篇关于“没有过载匹配此调用";在Angular 11中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆