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?
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
.
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