Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

angular - Type 'Headers' has no properties in common with type 'RequestOptionsArgs'?

I just made two important upgrades to our Angular 4 application and build tools:

  1. @angular/core ^4.1.3 => ^4.2.4 (and /http, /forms, etc)
  2. tslint ^5.3.2 => ^5.4.3

I have a Service which declares options like so:

@Injectable()
export class WorkOrderService {

    private headers: Headers = new Headers({ 'Content-Type': 'application/json' });
    private options: RequestOptions = new RequestOptions(this.headers);

    constructor(private http: Http) {}

    /* Methods ... */
}

The above now no longer validates tslint, throwing the following error:

error TS2559: Type 'Headers' has no properties in common with type 'RequestOptionsArgs'.

The source (@angular/http interface.d.ts:43) clearly allows for Headers as a RequestOptionsArgs:

/**
 * Interface for options to construct a RequestOptions, based on
 * [RequestInit](https://fetch.spec.whatwg.org/#requestinit) from the Fetch spec.
 *
 * @experimental
 */
export interface RequestOptionsArgs {
    url?: string | null;
    method?: string | RequestMethod | null;
    /** @deprecated from 4.0.0. Use params instead. */
    search?: string | URLSearchParams | {
        [key: string]: any | any[];
    } | null;
    params?: string | URLSearchParams | {
        [key: string]: any | any[];
    } | null;
    headers?: Headers | null;
    body?: any;
    withCredentials?: boolean | null;
    responseType?: ResponseContentType | null;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update for 4.3 HttpClient

The new syntax to be compatible with HttpClient, introduced in angular 4.3, is:

import { HttpClient, HttpHeaders } from "@angular/common/http";

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

No more RequestOptions: parameters are added using the new immutable HttpParams Map.

Pre 4.3 / Http

I just noticed that RequestOptions now requires you to explicitly pass named options as an object, like:

headers: Headers = new Headers({ 'Content-Type': 'application/json' });
options: RequestOptions = new RequestOptions({ headers: this.headers });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...