错误 TS2345:类型为"{ 标头:HttpHeaders; }"的参数不可分配给类型为"请求选项Args"的参数



这是我运行ionic serve时得到的结果。 我正在使用带有Angular 4的ionic8版本。

src/app/home/home.page.ts:60:77 - 错误 TS2345:类型为"{ 标头:HttpHeaders;}' 不可分配给类型的参数 'RequestOptionsArgs'.[吴] 属性"标头"的类型是 不相容的。[吴] 类型"HttpHeaders"缺少以下内容 类型"标头"的属性:forEach、值、toJSON、条目、 mayBeSetNormalizedName [ng] 60 this.http.post("http://localhost/android/Api.php?apicall=signup", 后数据,选项( [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);
});
}

包.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"
}

你应该这样做 导入: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);
});
}

相关内容

最新更新