Ionic storage issue

I am new to ionic 3.9.2. I want to add the storage to my project, but it showing an error like

“Typescript Error Cannot find name ‘storage’. D:/Ionic/SimBing/src/pages/home/home.ts storage.set(‘uname’, this.model.username); alert(storage.get(‘uname’)); // this.router.navigate([this.returnUrl]);”

Can you please help me to solve the issue… Currently I am checking in browser using ionic serve --l. Is there any issue related to this, I mean whether the storage will work only on device?

Thanks – Divya Eficaz

Storage will work fine on the browser, and on a device (although some people use NativeStorage on devices). You can see your storage set up in the chrome developer tools under :
Application --> Storage --> IndexedDB

Here is how you should implement Storage :

app.module.ts

    import { IonicStorageModule } from '@ionic/storage';
    ...

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

home.ts

  import {Storage} from '@ionic/storage'

  export class HomePage{
       
       constructor(public storage : Storage){
       }

       SetData(somedata){
             this.storage.set('MyData', somedata);
       }
       GetData(){
            this.storage.get('MyData').then((data)=>{
                var myData = data;
            }
       }
  }

Implementing storage like this should get rid of your error

Outside of the constructor you have to use this.storage.

2 Likes