This is then imported into any page that requires it and added to the constructor. It works fine. But I see examples using providers which look almost identical but the provider is added to the providers array in app.module.ts, what’s the difference?
Ah I think I’ve found the answer - leaving it here in case it help anyone else.
If you add it to the bootstrap then a single instance of the service will be created that all components in the application will share – making it a great way to share data between components. If you add the provider to an individual component, then a new instance will be created for just that one component.
That is how it used to work but it’s different now. A provider/service are the same thing, if you want to use dependency injection (injecting things through the constructor) you need to add the @Injectable decorator and add it to the providers array. If you are just creating a simple class that you just want to create new instances of then you don’t need to add it as a provider (you would just instantiate a new instance of it, i.e. let myThing = new Thing();
The terminology (provider vs. service) is irrelevant. What matters is where you declare it in a providers stanza. 99% of the time you want app-wide singletons, so this should be done in the app module. I frequently see posts here where services are declared in the providers stanza of a particular page. That will give you a separate instance per page, which is rarely what is wanted.