Ionic Storage - Angular new API

Hi there

As per new specs the Ionic Storage has a new API which requires you to invoke storage.create() before using it for the first time.

I am not clear about where to do this if I have a storage that is to be used across the app - do I need to do the create in app.component.ts, or do I actually need to create a service that wraps storage and provides for the initial create?

In the earlier API, the create was done through the module include at app.module.ts (implicitly).

Any thoughts would be very welcome!

Thx

How i understand the Change log and Readme for updating from 2.x to 3.x you have to call the create before trying to access the storage. So in app.component seems to be the right place for me

1 Like

Let’s see if the Ionic Team agrees

Hi, I’m trying out Ionic Storage. Just followed the steps in the readme. But I got the following error : NullInjectorError: No provider for Storage! How can I solve this?

Cheers
Henk

Well, maybe, and I am not saying out of experience, you need to import the StorageModule in the module the component belongs to? In addition to, or even instead of, in app.module.ts?

First, edit your NgModule declaration in src/app/app.module.ts or in the module for the page you’ll use the storage library in, and add IonicStorageModule as an import:

@Jurriens See my Issue wrote down here

That’s correct. The import fixes it!

Cool. And I believe, looking at the code of the module, it basically does what @EinfachHans explained - adding as provider. Question is what is the best practice.

IonicStorageModule is added as an Module. Still doesn’t work without the extra provider

1 Like

Hi there, I had a mistake in the docs, you need to make sure to import Storage from @ionic/storage-angular in your class. Adding it to the providers is not necessary.

I updated the readme: GitHub - ionic-team/ionic-storage: Ionic Storage module for Ionic apps

Also if you find more issues or have trouble please post it on the repo. I’m actively monitoring and can best help you there.

1 Like

Thx for this.
The OP is also asking guidance on the best practice for the create part. Also posted on the repo as PR.
Hope you can give your view on this as well.
Thx
Tom

The repo shows an example via a service. I prefer to instantiate in app.component.ts’ constructor. So I do not need to mirror the methods.

import { Component, OnInit } from '@angular/core';
import { Storage } from '@ionic/storage-angular';

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss'],
})
export class AppComponent implements OnInit {
  constructor( private storage: Storage  ) {
    this.storage.create();
    }
}