I just made two important upgrades to our Angular 4 application and build tools:
^4.1.3
=> ^4.2.4
(and /http, /forms, etc)^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;
}
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.
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 });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With