参数 'res' 隐式具有 'any' 类型。

35 浏览
0 Comments

参数 'res' 隐式具有 'any' 类型。

我正在尝试将Angular HTML页面的数据发送到MVC Core,并最终希望获得响应。因此,我使用了subscribe方法,但它显示了以下错误:\n

\n参数\'res\'隐式具有“any”类型。\n

\n这是我的代码:\n

import { Component } from '@angular/core';
import { Patient } from './app.model';
import {HttpClient} from "@angular/common/http"
import { Observable, observable } from 'rxjs';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
    patientObj: Patient = new Patient();
    constructor(public httpClient:HttpClient){}
    Add() { 
        alert(this.patientObj.id);
        var observable=this.httpClient.post("https://localhost:44331/Patient/SubmitPatient", this.patientObj);
        observable.subscribe(res=> this.Success(res), res=> this.Error(res));
    }
    Success(res) {
        alert(res);
    }
    Error(res) {
        alert(res);
    }
}

\n编辑1
\n我已经阅读了StackOverflow上的问题,并按照以下步骤进行了操作:\n//\"strict\": true,\n\n但对我没有起作用。

0
0 Comments

(Parameter 'res' implicitly has an 'any' type)这个问题出现的原因是在代码中没有明确指定参数'res'的类型,导致其隐式地被赋予了一个'any'类型。解决这个问题的方法有两种:

第一种方法是显式地使用'type any'来定义参数'res'的类型,如下所示:

observable.subscribe((res: any) => this.Success(res), (res: any) => this.Error(res));
Success(res: any) {
    alert(res);
}
Error(res: any) {
    alert(res);
}

第二种方法是在'tsconfig.json'文件中将'noImplicitAny'设置为'false',如下所示:

"noImplicitAny": false,

通过设置'noImplicitAny'为'false',可以关闭对隐式'any'类型的检查,从而解决这个问题。

感谢您的帮助,现在通过使用'res:any',问题已经解决了。

0