Are Angular2 services supposed to be stateless?

I come from the Java world, where services are usually designed for statelessness. Should there be services in Angular2 also stateless? Or can we just save the state because we don’t have to worry about concurrent threaded access, as in the Java example?

In https://angular.io/docs/ts/latest/guide/router.html#!#teach-authguard-to-authenticate in the AuthService class, the state is saved.

Is it just to simplify the example, or is it common practice? I know that services are created and updated for the area in which they are declared, but this will mean that I will need to take care of where the service was provided in order to know how long the state lasts.

+4
source share
1 answer

As a rule, it’s good to have stateless components and maintain state in the service, especially in components added by the router, so moving from it and then returning to the route does not discard the data.

Therefore, to your question: Services should not be stateless. They often happen, but this is not required.

You can use the NGRX Store to save this state, but also the service.

I will need to take care of where the service was provided to know how long the condition lasts.

, , . . , , @NgModule() ( ), , end .

+8

Source: https://habr.com/ru/post/1670469/


All Articles