I'm trying to change the request options but it's deprecated. I can't find the option for this.
Any help?
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/mon/http'
import { HttpHeaders} from "@angular/mon/http";
import {RequestOptions} from "@angular/http";
@Injectable({
providedIn: 'root'
})
export class UserService {
private baseUrl:string = 'http://localhost:8080/api';
private headers = new HttpHeaders({'Content-Type':'application/json'});
private options = new RequestOptions({headers:this.headers});
constructor() { }
}
I'm trying to change the request options but it's deprecated. I can't find the option for this.
Any help?
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/mon/http'
import { HttpHeaders} from "@angular/mon/http";
import {RequestOptions} from "@angular/http";
@Injectable({
providedIn: 'root'
})
export class UserService {
private baseUrl:string = 'http://localhost:8080/api';
private headers = new HttpHeaders({'Content-Type':'application/json'});
private options = new RequestOptions({headers:this.headers});
constructor() { }
}
Share
Improve this question
edited May 15, 2018 at 9:57
Ivaylo Valchev
10.4k6 gold badges59 silver badges84 bronze badges
asked May 15, 2018 at 9:06
user9729328user9729328
4
- from which version did you switch to angular 6? – Benedikt Schmidt Commented May 15, 2018 at 9:07
- angular 0....... – user9729328 Commented May 15, 2018 at 9:08
- Possible duplicate of RequestOptions migration Angular 5 – Muhammed Albarmavi Commented May 15, 2018 at 9:25
-
I don't think his intention was to intercept every call to set default headers, but rather to switch from the deprecated
RequestOptions
to a current solution. – Benedikt Schmidt Commented May 15, 2018 at 10:06
2 Answers
Reset to default 3I think this is just an import problem, because they moved these options to another place. The documentation states:
use @angular/mon/http instead
So I guess you just have to import the options from @angular/mon/http
instead of @angular/http
EDIT
I should have looked a bit closer. Headers can now be send a bit differently than before, you don't have to use RequestOptions
anymore, just pack it up as a simple object. In your case it could look like this:
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
Then you can use these options with your http methods. Here's an example from angular's fundamentals:
addHero (hero: Hero): Observable<Hero> {
return this.http.post<Hero>(this.heroesUrl, hero, httpOptions)
.pipe(
catchError(this.handleError('addHero', hero))
);
}
Deprecated APIs and Features
Headers -> HttpHeaders
RequestOptions -> HttpRequest
HttpModule -> HttpClientModule
Old
import {HttpModule, Headers, RequestOptions} from "@angular/http";
New
import { HttpClientModule, HttpRequest, HttpHeaders } from "@angular/mon/http";
Refer the following URL for more details: https://angular.io/guide/deprecations
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744784544a4593543.html
评论列表(0条)