Can I call api service in background in angular 7 while I route to different component

Viewed 389

Api service call in the background without destroying service while routing other components.

e.g:-You are on the homepage and you call a service that will upload a big file but in the meantime you want to go another component and that service will run in the background and when it success it will give some message.

Thank you :)

1 Answers

Services are provided in a module. If you destroy the module, you destroy the service.

By using

@Injectable({ providedIn: 'root' }) 

You declare your service in the root module (the one that manages your application). By doing so, your service becomes a singleton : it only gets destroyed if your app gets destroyed, and there's only a single instance of it.

Services (and especially singletons) lifecycle are different than component. They kind of have a life of their own and don't really care about the routing.

So yes, you can easily do that.

Now, as a new Stack Overflow user, you should be aware that this website is made for questions about code errors & bugs. This kind of question doesn't really belong here. Be sure to read How to ask and provide a Minimal, Complete & Verifiable Example of your issue

Related