Providers allow you to create a self-contained service that provides some kind of functionality to your application. The role of a provider might include things like fetching data from a server, performing operations on data, sharing data, providing a set of complicated mathematical operations, and so on.
In general, we use providers to do the heavy lifting in our applications. By using a provider, we abstract the functionality it provides away from a specific component, which makes the codebase more maintainable and allows us to reuse that same provider in multiple different places in the application (or even in a completely different application).
In this video tutorial, I walk through a simple of example of when it would make sense to create a provider and how to do it.
Here’s the video:
- You can create a new provider using the command:
ionic g provider MyProvider
- You must import the provider into app.module.ts and add it to the
- You must import the provider into any component that uses it
- You must inject the provider in the
constructorof any component that is using it