【问题标题】:ionic 3 native storage read value storedionic 3本机存储读取值存储
【发布时间】:2018-04-23 02:53:44
【问题描述】:

您好,

在我的代码中,我尝试了写在本机存储插件页面上的代码:Native Storage

    import { NativeStorage } from '@ionic-native/native-storage';

constructor(private nativeStorage: NativeStorage) { }

...

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

this.nativeStorage.getItem('myitem')
  .then(
    data => console.log(data),
    error => console.error(error)
  );

当我启动我的 android 模拟器设备时,控制台会发回这个:

[00:02:01]  console.log: Stored item! 
[00:02:01]  console.log: [object Object] 

我想找到一种解决方案来读取存储的信息。因为我想使用存储在本机存储中的值在外部页面上创建条件,但我不能。示例“如果存储在名称 Vibrator 的本机存储中的值 == 为 true,则我们启动此函数”。我正在寻找读取值的方法。你能帮帮我吗?

谢谢

【问题讨论】:

    标签: angular typescript cordova-plugins ionic3 cordova-nativestorage


    【解决方案1】:

    要读取值,可以这样调用:

    console.log(data.property);
    console.log(data.anotherProperty);
    

    参考插件github更清楚了解:https://github.com/TheCocoaProject/cordova-plugin-nativestorage

    【讨论】:

      【解决方案2】:

      您无法获得这样的值,因为它是Async 操作。您只是尝试copy/paste 文档的代码。这不是真正的使用案例。这只是一个例子。

      这是真正的用例模拟。您存储的值如下所示。

      my-first-page.ts

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

      在第二页之后,您可以检索如下值。

      my-second-page.ts

      this.nativeStorage.getItem('myitem')
        .then(data => {
            console.log(data);
         },
          error => console.error(error)
        );
      

      注意:如果您需要进一步的帮助,请告诉我。

      【讨论】:

      • 我完全按照你说的做了,但没有运气。请帮忙
      【解决方案3】:

      我完全按照你的解释做了,我添加了我想要的“财产”价值。我输入了 Home.ts

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

      parameter.ts 我放了

      this.nativeStorage.getItem('myitem')
        .then(
          data => console.log(data.property),
          error => console.error(error)
        );
      

      当我运行模拟器时,我的控制台会告诉我我什么时候掉进了 home.ts

      [10:30:06]  console.log: Stored item! 
      [10:30:06]  console.log: deviceready has not fired after 5 seconds. 
      [10:30:06]  console.log: Ionic Native: deviceready event fired after 4017 m
      s 
      

      当我点击页面parameter.ts这里是我得到的结果

      [10:56:17]  console.log: value 
      [10:56:17]  console.error: [object Object]
      

      我很清楚“财产”的价值等于“价值”。

      谢谢大家

      【讨论】:

        猜你喜欢
        • 2018-04-27
        • 1970-01-01
        • 2018-04-05
        • 1970-01-01
        • 2018-03-19
        • 2018-03-10
        • 1970-01-01
        • 1970-01-01
        • 2020-01-23
        相关资源
        最近更新 更多