你好,
在我的代码中,我尝试了在本机存储插件的页面上编写的代码,这里是:本机存储
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的本机存储上的值为== to true,则启动此函数”。我正在寻找读取值的方法。你能帮帮我吗?
谢谢
发布于 2017-11-10 00:25:35
若要读取该值,请如下所示:
console.log(data.property);
console.log(data.anotherProperty);请参阅插件github以获得更清晰的理解:https://github.com/TheCocoaProject/cordova-plugin-nativestorage
发布于 2017-11-10 06:20:27
您无法获得这样的值,因为它是一个Async operation.You,它刚刚尝试过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)
);在第2页之后,您可以检索如下所示的值。
my-second-page.ts
this.nativeStorage.getItem('myitem')
.then(data => {
console.log(data);
},
error => console.error(error)
);注:,如果您需要进一步的帮助,请告诉我。
发布于 2017-11-10 09:47:10
我做了你解释的,我增加了我想要的财产价值。我输入了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]我很好地得到了“财产”的价值,它等于“价值”。
谢谢你们
https://stackoverflow.com/questions/47213258
复制相似问题