Add Provider to app.module.ts so is persistent across all components in Android


#1

I have a DataService provider for connecting to a database.
I store some data retrieved in that Provider Service.
When I use ionic serve, I have no problem accessing the saved data in that service from other components, simply by importing the DataService into the component, like so:

import { DataService } from '../../providers/data-service';

However, when I ionic run android, I get the no provider for t error.
I can resolve this by adding the DataService as a provider in the component, like so:

@Component({
  selector: 'page-demo',
  templateUrl: 'demo.html',
  providers: [DataService]
})

But, then the data in the DataService provider is not persistent - I cannot access the saved data in the DataService from another component because it is essentially recreating the service, not accessing the same instantiation of it. ( I think )

What is the correct way to add a Provider/Service in the app.module.ts file in order to be abble to access data within the Provider from any component - AND it works on Android?

Here is my example: ( DataService is the one that gets the no provider for t error if not added as a provider in the component itself ) ( AuthService works great )

    providers: [
        DataService,
        Storage,
        AuthHttp,
        AuthService,
        {
          provide: AuthHttp,
          useFactory: getAuthHttp,
          deps:[Http]
        }
      ]

#2

To answer my own…
There must have been something else in the mix that was thwarting the above from working.
I started a new project and it works fine.

Put the Provider in the app.module.ts @NgModule providers array.
No need for adding Provider in Components.

Unfortunately, cannot peg down exactly what was causing the error, so moving on.