错误TS2345:类型为'{headers:HttpHeaders;的参数}”不能分配给“ RequestOptionsArgs”类型的参数

时间:2020-01-28 14:21:30

标签: angular typescript ionic4

这是我运行ionic serve时得到的结果。 我在Angular 8中使用ionic4版本。

src / app / home / home.page.ts:60:77-错误TS2345:类型为'{ 标头:HttpHeaders; }'不能分配给类型的参数 'RequestOptionsArgs'。 [ng]属性“标题”的类型是 不相容。 [ng]类型“ HttpHeaders”缺少以下内容 “标头”类型的属性:forEach,值,toJSON,条目, mayBeSetNormalizedName [ng] 60 this.http.post(“ http://localhost/android/Api.php?apicall=signup”, postData,options)[ng]

sendPostRequest() {

    let headers = new HttpHeaders();

    headers.append("Accept", 'application/json');
    headers.append('Content-Type', 'text/plain');
    let options = {
        headers: headers
    }


    /*
         if (this.authService.checkAuth()) {
             const token = 'something';
             headers = headers.set('Authorization', `Bearer ${token}`);
         }
      const authReq = req.clone({ headers });*/


    let postData = {

        "username": "Customer004",
        "email": "customer004@email.com",
        "password": "0000252525",
        "gender": "male",
    }

    /*this.http.post("http://localhost/android/Api.php?apicall=signup", postData, requestOptions)
      .subscribe(data => {
        console.log(data['_body']);
       }, error => {
        console.log(error);
      });*/


    var options = {
        headers: new HttpHeaders({
            'Content-Type': 'application/json'
        })
    };


    this.http.post("http://localhost/android/Api.php?apicall=signup", postData, options)
        .subscribe(data => {
            console.log(data);
        });

}

package.json

{
  "name": "registration",
  "version": "0.0.1",
  "author": "Ionic Framework",
  "homepage": "https://ionicframework.com/",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "~8.1.2",
    "@angular/core": "~8.1.2",
    "@angular/forms": "~8.1.2",
    "@angular/http": "^7.2.16",
    "@angular/platform-browser": "~8.1.2",
    "@angular/platform-browser-dynamic": "~8.1.2",
    "@angular/router": "~8.1.2",
    "@ionic-native/core": "^5.0.0",
    "@ionic-native/splash-screen": "^5.0.0",
    "@ionic-native/status-bar": "^5.0.0",
    "@ionic/angular": "^4.7.1",
    "core-js": "^2.5.4",
    "rxjs": "~6.5.1",
    "tslib": "^1.9.0",
    "zone.js": "~0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/architect": "~0.801.2",
    "@angular-devkit/build-angular": "~0.801.2",
    "@angular-devkit/core": "~8.1.2",
    "@angular-devkit/schematics": "~8.1.2",
    "@angular/cli": "~8.1.2",
    "@angular/compiler": "~8.1.2",
    "@angular/compiler-cli": "~8.1.2",
    "@angular/language-service": "~8.1.2",
    "@ionic/angular-toolkit": "^2.1.1",
    "@types/jasmine": "~3.3.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^8.9.5",
    "codelyzer": "^5.0.0",
    "jasmine-core": "~3.4.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.1.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.15.0",
    "typescript": "~3.4.3"
  },
  "description": "An Ionic project"
}

1 个答案:

答案 0 :(得分:1)

您应该导入: import { Http, Headers, RequestOptions } from '@angular/http';

从http进行DI注入是可以的。然后只需使用RequestOptions,如下所示:

sendPostRequest() {
    let headers = new Headers({'Content-Type': 'application/json'});
    // headers.append('Content-Type', 'text/plain');
    // I didn't understand if you need this append.

    let options = new RequestOptions({ headers: headers });

    const postData = {
        "username": "Customer004",
        "email": "customer004@email.com",
        "password": "0000252525",
        "gender": "male",
    }

    this.http.post("http://localhost/android/Api.php?apicall=signup", postData, options)
        .subscribe(data => {
            console.log(data);
        });
}