2 ms·
Interestingly, they explain some of the reasoning behind the extra info in the docs. WHY WRITE A SERVICE? This example is so simple that it is tempting to writ
by rileymat2 7y ago
Interestingly, they explain some of the reasoning behind the extra info in the docs.
WHY WRITE A SERVICE?
This example is so simple that it is tempting to write the Http.get() inside the component itself and skip the service. In practice, however, data access rarely stays this simple. You typically need to post-process the data, add error handling, and maybe some retry logic to cope with intermittent connectivity.
The component quickly becomes cluttered with data access minutia. The component becomes harder to understand, harder to test, and the data access logic can't be re-used or standardized.
That's why it's a best practice to separate presentation of data from data access by encapsulating data access in a separate service and delegating to that service in the component, even in simple cases like this one.