I'm currently developing multiple applications using Angular 5. My aim is to adhere to all the dos and don'ts of Angular.
However, I'm facing some confusion regarding a few things.
1) What is the difference between this...
this._http.get<User>(this._ubiRest.servicesUrls.getUser)
...and this?
this._http.get(this._ubiRest.servicesUrls.getUser)
If I still have to use the map method to convert it to type User (or maybe not?).
2) In a service, would it be preferable to do this...
getUserData(): Observable<User> {
return new Observable((observable) => {
if (!!this._loggedUser) {
observable.next(this._loggedUser);
observable.complete();
}
else {
this._http.get(this._ubiRest.servicesUrls.getUser)
.map(this._extractData)
.subscribe(user => {
this._loggedUser = user;
observable.next(user);
observable.complete();
}, this._handleError);
}
})
}
...or this?
getUserDataX(): Observable<User> {
if (!!this._loggedUser) {
return new Observable(observable => {
observable.next(this._loggedUser);
observable.complete();
});
}
else {
return this._http.get<User>(this._ubiRest.servicesUrls.getUser)
.map(this._extractData)
.catch(this._handleError);
}
}