IonicStorage for Angular in Ionic 7

Hi everyone,
In my old app (<= v6), I used standard configuration for @ionic/storage-angular dependency but in ionic 7 src/app/app.modules.ts file is not present. How works in ionic7?

In my old apps:

npm install @ionic/storage-angular

In src/app/app.modules.ts add these imports:

IonicStorageModule.forRoot()

In Ionic 7 app.modules.ts is not present. I created my CustomStorageService (correct name is: MgdStorageService) that need to use storage

import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage-angular';
import { from, Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class MgdStorageService {

  private _storage!: Storage;

  constructor(
    private storage: Storage
  ) { 
    this.init();
  }

  async init() {
    const storage = await this.storage.create();
    this._storage = storage;
  }

  ...
}

but I receive this error:

NullInjectorError: R3InjectorError(Standalone[FolderPage])[MgdStorageService -> MgdStorageService -> MgdStorageService -> Storage -> Storage]

What is the best solution to enable injection of Storage Service?

Thanks

Imported via main.ts file. Is it correct?

bootstrapApplication(AppComponent, {
  providers: [
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
    importProvidersFrom(IonicModule.forRoot({})),
    importProvidersFrom(HttpClientModule),
    importProvidersFrom(IonicStorageModule.forRoot()),
    provideRouter(routes),
  ],
});
1 Like

this is the solution to import ?

yes. it works fine.

L

How do you do this when not using the standalone