Local storage in ionic 2 error

前端 未结 2 751
[愿得一人]
[愿得一人] 2020-12-22 05:43

I try to use this in my Ionic 2 application : https://ionicframework.com/docs/v2/storage/

I already run

cordova plugin add cordova-sqlite-storage --         


        
相关标签:
2条回答
  • 2020-12-22 05:57

    Since Ionic 2.2.0, it's recommended to use @ionic/storage version 2.0.0. Configuration in app.modules.ts has changed since the previous version. The error occurs if you haven't changed everything in the right way.

    In app.modules.ts do following changes:

    1. Remove Storage from providers
    2. Change import statement:

      from: import { Storage } from '@ionic/storage';

      to: import { IonicStorageModule } from '@ionic/storage';

    3. Add the following to the imports array:

      IonicStorageModule.forRoot()

    The import array should look like below:

    imports: [
      IonicModule.forRoot(MyApp),
      IonicStorageModule.forRoot()
    ],
    



    NOTE: Do not make any change in imports of Storage in any other files.

    0 讨论(0)
  • 2020-12-22 05:57

    Your app.module.ts should look like,

    import { Storage } from '@ionic/storage';
    
    export function provideStorage() {
      return new Storage(['sqlite', 'websql', 'indexeddb'], { name: 'database_name' });
    }
    
    @NgModule({
      declarations: [
      ],
      imports: [
        IonicModule.forRoot(MyApp)
      ],
      bootstrap: [IonicApp],
      entryComponents: [
      ],
      providers: [
        { provide: Storage, useFactory: provideStorage }, Storage
      ]
    })
    

    You have to use set() and get() methods to store and retrieve data in your pages. Have a look at Ionic 2 Storage Tutorial Blog with example video. Hope this helps

    0 讨论(0)
提交回复
热议问题