'Subscribe Deprecated
I'm working on learning Angular and I am getting a warning that Subscribe is being deprecated. My method is as follows:
login() {
this.accountService.login(this.model).subscribe(response => {
console.log(response);
}, error => {
console.log(error);
})
}
Subscribe has been crossed out and I'm not sure how to re write this to work using the new updated way of doing so. Is anyone able to provide me with the details on how to do this using the observer pattern?
Solution 1:[1]
So the syntax you can switch to is:
login() {
this.accountService.login(this.model).subscribe({
next: response => {
console.log(response);
},
error: error => {
console.log(error);
}
});
}
We can now pass like this into subscriptions (there is also a complete property you can use for when the observable completes). Note that, each of these properties on the passed object are optional and you can use whichever of them you like.
Some documentation showing this subscribe method here: https://rxjs.dev/api/index/class/Observable#subscribe
Partial<Observer<T>> is what we are trying to satisfy within that method, where Observer<T> is defined as:
interface Observer<T> {
next: (value: T) => void
error: (err: any) => void
complete: () => void
}
which can be found here: https://rxjs.dev/api/index/interface/Observer
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
