Can someone explain this piece of code to me?
getAll() {
return this.http.get<User[]>(`${environment.config.apiUrl}/users`);
}
This code is taken from user.service.ts of this stackblitz https://stackblitz.com/edit/authenticationdemo?file=src%2Fapp%2Fservices%2Fuser.service.ts I know environment.config is a variable declared in environments/environment.ts but I don't understand its meaning:
export const environment = {
production: false,
config: <any> ""
};
also I don't understand 'apiUrl' what kind of method is it, where is it referenced? I am looking for explanations, documentation about it is fine too, I have tried but I have not found anything on the official documentation. Does this kind of representation of 'http.get' code have a name? Thanks friends
Let's get it step by step.
return this.http.get<User[]>(`${environment.config.apiUrl}/users`);
http
is an instance of HttpClient injected from angular in the service
get
is a function that will perform an http GET request to the target parameter url
The <User[]>
is a generic type that says that the http get will return an observable of those Observable and it's there to assure type safety.
The environment.config
is of type any. That means that during compile time, it is not type safe and you can access any property, even if it exists or not.
When you access it, it is undefined value, which does not throw an error. Since the service is fake and the value is irrelevant then the code works