Header 未赋值
Header is not assigned value
这是我的代码
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import { Headers } from '@angular/http';
@Injectable()
export class DataproviderProvider {
constructor(public http: HttpClient) {
console.log('Hello DataproviderProvider Provider');
}
// registering the user
resgisterData(registerdata) {
let headers = new Headers({'Content-Type': 'application/json'});
this.http.post('http://localhost:3000/api/contact',registerdata,{headers: headers})
.map(res => res.json());
}
}
我收到类似
的错误消息
Argument of type '{ headers: Headers; }' is not assignable to
parameter of type '{ headers?: HttpHeaders | { [header: string]:
string | string[]; }; observe?: "body"; params?: Ht...'. Types of
property 'headers' are incompatible.
Type 'Headers' is not assignable to type 'HttpHeaders | { [header: string]: string | string[]; }'.
Type 'Headers' is not assignable to type '{ [header: string]: string | string[]; }'.
Index signature is missing in type 'Headers'.
在 visual studio 代码中,它在 {headers: headers}
处显示错误
似乎 HttpHeaders 的类型而不是 Headers。
这就是 Type 'Headers' is not assignable to type 'HttpHeaders' 的意思。
你应该这样
let header = new HttpHeaders();
header.append('Content-Type', 'application/json');
这是我的代码
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import { Headers } from '@angular/http';
@Injectable()
export class DataproviderProvider {
constructor(public http: HttpClient) {
console.log('Hello DataproviderProvider Provider');
}
// registering the user
resgisterData(registerdata) {
let headers = new Headers({'Content-Type': 'application/json'});
this.http.post('http://localhost:3000/api/contact',registerdata,{headers: headers})
.map(res => res.json());
}
}
我收到类似
的错误消息Argument of type '{ headers: Headers; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'. Types of property 'headers' are incompatible. Type 'Headers' is not assignable to type 'HttpHeaders | { [header: string]: string | string[]; }'. Type 'Headers' is not assignable to type '{ [header: string]: string | string[]; }'. Index signature is missing in type 'Headers'.
在 visual studio 代码中,它在 {headers: headers}
处显示错误似乎 HttpHeaders 的类型而不是 Headers。 这就是 Type 'Headers' is not assignable to type 'HttpHeaders' 的意思。
你应该这样
let header = new HttpHeaders();
header.append('Content-Type', 'application/json');