April 2018
Intermediate to advanced
390 pages
8h 46m
English
In general, if you want to use a common service across multiple pages, you must inject it at the highest level. In this example, you put UserService as a dependency at the start of app.module.ts, as follows:
providers: [UserService]
After that, other pages within the app can start using this common service without having to reinject it. The main reason is that, whenever you inject a service or class, it will instantiate a new object, which ends up erasing all of the existing data in the memory. If you want the data to persist across the pages, it should be in the parent app to avoid reinjection.
To use the UserService on each page, you just need to import it, as illustrated in the following code:
import { UserService } from ...