How can we use the same instance of the service in Angular?

557    Asked by JohnIngerson in Python , Asked on Jan 25, 2020
Answered by Arun Singh

 We can create the service with a singleton pattern. So, if you are creating the instance of the service without making it singleton then every time it will use the different memory location and create the new instance but in case of singleton pattern the same instance will be used. We can make the service singleton by two ways:

Using providedIn: ’root’ property inside the @Injectable.

Include the service in AppModule or in a module that is only imported by the AppModule.



Your Answer

Interviews

Parent Categories