Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 2 Provider

I am new to Ionic 2 and am trying to get through any of the tutorials online that show how to add a provider. It seems that Ionic has changed the app structure the is generated. can some please give me an example of how to do this with the current Ionic 2 app structure? Everywhere I try to import and add my provider to the page class (Constructor and @Component page decorator), I get an error that it cannot be found. All I am trying to do is follow this tutorial with the current Ionic 2 app structure.

like image 496
jdubicki Avatar asked Dec 15 '22 02:12

jdubicki


2 Answers

In `app.module.ts'

import { PeopleService } from '../providers/people-service';
@NgModule({
  declarations: [
    // Declarations commented out for brevity
  ],
  imports: [
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    // Entry Components commented out for brevity
  ],
  providers: [PeopleService] // Add PeopleService provider
})

and then on 'home-page.ts'

import {PeopleService} from '../providers/people-service/people-service';

export class HomePage {
public people: any;

    constructor(public peopleService: PeopleService){

      }
    }
like image 188
Matt Avatar answered Feb 18 '23 09:02

Matt


You could add a provider by running

> ionic g provider storage-provider

This will generate a ready template

And then add this using import to your app.modules.ts file

> import { StorageProvider } from '../providers/storage-provider';
> import { IonicApp, IonicModule  } from 'ionic-angular';

And then in the same app.modules.ts file, you go to the providers section at the very bottom in the file Add the StorageProvider class

providers: [AuthProvider,  UtilProvider,**StorageProvider**  ],
like image 41
Magnus Melwin Avatar answered Feb 18 '23 09:02

Magnus Melwin