2017-04-26 108 views
0

“删除”我使用进口{存储}从“@离子/存储”存储运行时错误无法读取属性未定义

给运行时错误无法读取属性未定义时使用“删除” this.storage.remove( '钥匙'); Error

这是我的功能代码是我过去:this.storage.remove

logout(): void { 
      this.storage.remove(this.HAS_LOGGED_IN); 
      this.storage.remove('email'); 
      this.events.publish('user:logout'); 
      }; 
+0

存储在哪里定义? – devqon

+0

是否将@ ionic/storage添加到app.module.ts? –

+0

yes storage.set()和storage.get()工作正常 – moreshwar

回答

0

你必须注入在构造一个storage对象:

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

constructor(private storage: Storage) { 

} 

logout(): void { 
    this.storage.remove(this.HAS_LOGGED_IN); 
    this.storage.remove('email'); 
    this.events.publish('user:logout'); 
}; 

注意您还必须在您的应用程序中注册存储模块:

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

@NgModule({ 
    declarations: [ 
     // ... 
    ], 
    imports: [ 
     IonicModule.forRoot(MyApp), 
     IonicStorageModule.forRoot() 
    ], 
    bootstrap: [IonicApp], 
    entryComponents: [ 
     // ... 
    ], 
    providers: [] 
}) 
export class AppModule {} 

this documentation

+0

是@devqon我在app.module.ts中导入了IonicStorageModule – moreshwar

0

安装离子本地包> $ NPM安装离子本地--save

,并用它喜欢:

import { Storage } from 'ionic-native 

//and in component class call it like this 

Storage.remove('email'); 

你不需要,如果注入它,你没有使用包装

相关问题