我在 tabs1 上有一张 map 。
我单击标记并发送本地存储标记标题并发送到 tab2。那工作很好。
that.passingData(dogwalkMarker.valueOf().parkid);
passingData(something: string) {
localStorage.setItem('passingData', JSON.stringify(something));
console.log(something);
但是当我第一次加载返回后的 tab2 时,出现了问题。在我加载应用程序上的任何页面并加载选项卡 2 后,它可以正常工作并返回良好!
我不明白我在哪里做错了。谢谢。
我在选项卡 2 上有以下代码。
scanOtopark() {
this.selectedOtopark = {};
let x = JSON.parse(localStorage.getItem('passingData'));
this.selectedOtopark = this.otoparks.find(otopark => otopark.parkqr === x);
if (this.selectedOtopark !== undefined) {
this.otoparkFound = true;
const toast = this.toastCtrl.create({
message: "good",
duration: 3000,
position: 'top'
});
toast.present();
console.log(this.selectedOtopark);
} else {
this.otoparkFound = false;
const toast = this.toastCtrl.create({
message: 'something goes wrong',
duration: 3000,
position: 'top'
});
toast.present();
this.selectedOtopark = {};
}
最佳答案
您的本地存储似乎存在异步
问题。因此我强烈建议使用 native storage module 。由于它支持开箱即用的 Promise,因此您可以在异步情况下轻松使用它。如果您也安装 SQLite,它可以在所有平台上正常工作(尤其是 iOS 设备)需要。)
文档中的示例:
// set a key/value
storage.set('name', 'Max');
// Or to get a key/value pair
storage.get('age').then((val) => {
console.log('Your age is', val);
});
更新:
在首次加载
时,您需要使用:
this.platform.ready().then(() => {
});
更新2:
x: any;
scanOtopark() {
this.selectedOtopark = {};
this.platformCtrl.ready().then(() => {
this.storageCtrl.get('passingData').then((data) => {
this.x= data;
this.selectedOtopark = this.otoparks.find(otopark => otopark.parkqr === this.x);
if (this.selectedOtopark !== undefined) {
this.otoparkFound = true;
const toast = this.toastCtrl.create({
message: "good",
duration: 3000,
position: 'top'
});
toast.present();
console.log(this.selectedOtopark);
} else {
this.otoparkFound = false;
const toast = this.toastCtrl.create({
message: 'something goes wrong',
duration: 3000,
position: 'top'
});
toast.present();
this.selectedOtopark = {};
}
});
});
关于angular - ionic 3 上的本地存储和选项卡菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46761846/