Communication between components using services
A characteristic that we must understand about Angular services is that, by default, every service instantiated by the dependency injection mechanism has the same reference; that is, a new object is not created, but reused.
This is because the dependency injection mechanism implements the singleton design pattern to create and deliver the objects. The singleton pattern is a design pattern of the creational type and allows the creation of objects whose access will be global in the system.
This characteristic is important for the service because, as the service deal with reusable business rules, we can use the same instance between components, without having to rebuild the entire object. In addition, we can take advantage of this characteristic and use services as an alternative for communication between components.
Let’s change our gym diary so that the ListEntriesComponent
component receives the initial list by service...