【问题标题】:Runtime Error Cannot read property 'remove' of undefined运行时错误无法读取未定义的属性“删除”
【发布时间】:2017-04-26 22:10:15
【问题描述】:

我正在使用 import { Storage } from '@ionic/storage' storage

使用 this.storage.remove('key'); 时给出运行时错误 Cannot read property 'remove' of undefined

这是我使用的功能代码:this.storage.remove

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

【问题讨论】:

  • 存储在哪里定义?
  • 您是否将 @ionic/storage 添加到 app.module.ts 中?
  • 是 storage.set() 和 storage.get() 工作正常
  • logout()函数是回调函数吗?
  • 没有它的不回调函数

标签: ionic-framework ionic2


【解决方案1】:

你必须在构造函数中注入一个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

【讨论】:

  • 是 @devqon 我在 app.module.ts 中导入了 IonicStorageModule
【解决方案2】:

安装离子原生包> $ npm install ionic-native --save

并像这样使用它:

import { Storage } from 'ionic-native

//and in component class call it like this

Storage.remove('email');

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    相关资源
    最近更新 更多