【问题标题】:Native Storage not working on my Ionic app本机存储不适用于我的 Ionic 应用程序
【发布时间】:2020-11-03 21:23:42
【问题描述】:
import { NativeStorage } from '@ionic-native/native-storage/ngx';

...

constructor(
  private nativeStorage: NativeStorage
) {
}

setValue() {
  console.log('start')
  this.nativeStorage.setItem('prop', 'value')
  this.nativeStorage.setItem('prop2', 'value2')
  console.log('end')
}
...

当我调用 setValue() 时 console.log('start') 运行,但第二个不起作用

如果我用 .then 调用,那么控制台中仍然没有任何内容

this.nativeStorage.setItem('myitem', {property: 'value', anotherProperty: 'anotherValue'})
.then(
  () => console.log('Stored item!'),
  error => console.error('Error storing item', error)
);

【问题讨论】:

  • 你在providers的app.modules.ts中添加了吗?
  • 是提供者:[StatusBar,SplashScreen,NativeStorage,{提供:RouteReuseStrategy,useClass:IonicRouteStrategy}],
  • 你是在真机上测试吗?
  • 是的。问题不在于。在我的应用程序中,我调用 login(user: User): Observable{ this.nativeStorage.setItem('1', '1') console.log('after') return this.http.get(this.url+' /auth.php?email='+user.email+'&password='+user.password) .pipe(tap(this.setToken), catchError(this.handleError.bind(this)) ) } naviteStorage 在这里工作但是当我调用 private setToken(response: AuthResponse | null) { this.nativeStorage.setItem('1', '1') console.log('after') } 我什么也没得到
  • 如果您自己修复了,请将其发布为答案并接受答案

标签: ionic-framework ionic4 ionic-native cordova-nativestorage


【解决方案1】:

代码一切正常

问题出在处理从 .pipe(tap(this.setValue())) 调用 setValue() 的方法中,我无法调用 this.nativeStorage

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多