首页 文章

运行时错误无法读取未定义的属性“删除”

提问于
浏览
0

我正在使用**import {.1'来自'@ionic/storage'**存储

给运行时错误当使用this.storage.remove('key')时,无法读取未定义的属性'remove';
错误

这是我的功能代码,其中我 used:this.storage.remove

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

2 回答

  • 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 {}
    

    这个文件

  • 0

    安装 ionic-native 包> $ npm install ionic-native --save

    并使用它像:

    import { Storage } from 'ionic-native
    
    //and in component class call it like this
    
    Storage.remove('email');
    

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

相关问题