Reputation: 634
Given the source Angular Service code
@Injectable()
export class HomeContentService {
constructor(private http: HttpClient) { }
getContent(): Observable<string> {
let fileURI = '/assets/content/home.md';
let opt = { responseType: 'text' };
return this.http.get<string>(
fileURI,
opt
);
}
Calling getContent()
should return an Observable<string>
. However, when I run ng serve
, I get Type 'string' is not assignable to type 'json'
.
After a few tries webpack seems to compile fine without me doing any changes, but the problem happens again anytime I close the process and try to ng serve
again.
I guess there's something wrong with my setting responseType: 'text'
, but I don't know what it is. Googling wasn't helpful either.
Any ideas?
Upvotes: 8
Views: 13987
Reputation: 310
Try let opt = { responseType: 'text' as 'text' };
Explanation: https://github.com/angular/angular/issues/18586
Upvotes: 3
Reputation: 62213
You have to inline the options as this is the only way that the TypeScript transpiler can infer that you are wanting to return type Observable<string>
.
getContent(): Observable<string> {
let fileURI = '/assets/content/home.md';
return this.http.get(fileURI, { responseType: 'text' });
}
See also Http ResponseType cannot be set #18586, refer to the comments made by alxhub
.
Upvotes: 10
Reputation: 732
i think u are not specifying map. Try this:
getContent ( ) : Observable< string > {
return this.http.get( YOUR REF )
.map((res:Response) => res.text())
.catch((error:any) => Observable.throw(error.json().error || 'Server error'));
}
in res.text u specify the type, if the response was json ll be "res.Json()"
Sry for the Bad english
Upvotes: 0