我在tabs1上有一张地图。
我单击标记并发送localstorage标记标题并发送到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 = {};
}
答案 0 :(得分:1)
您的本地存储似乎存在async
问题。所以我强烈建议您使用native storage module。由于它支持开箱即用的承诺,因此您可以在async
情况下轻松使用它。如果您也安装了SQLite,它可以在所有平台上正常工作(尤其需要iOS
设备。)
来自doc:
的示例 // 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);
});
<强>更新强>
在first time load
上你需要使用:
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 = {};
}
});
});
答案 1 :(得分:1)
<强>解强>
这是我的错,因为我定义了我在构造函数中获取数据库代码。因为该页面需要加载以获取数据。
忘记了代码部分
constructor(private navCtrl: NavController,
...
...
...
...
private storageCtrl: Storage,
private platformCtrl: Platform,
) {
this.firebaseProvider.getOtoparks().subscribe((response) => {
this.otoparks = response;
console.log(this.otoparks);
});}
所以我在函数中导入该代码并且它有效!
<强> TAB1 强>
passingData(something: string) {
this.platformCtrl.ready().then(() => {
this.storageCtrl.set('passingData', something);
console.log(something);
});
<强> TAB2 强>
passingData: any;
scanOtopark() {
this.platformCtrl.ready().then(() => {
this.storageCtrl.get('passingData').then((data) => {
this.passingData = data;
this.firebaseProvider.getOtoparks().subscribe((response) => {
this.otoparks = response;
this.selectedOtopark = this.otoparks.find(otopark => otopark.parkqr === this.passingData);
if (this.selectedOtopark !== undefined) {
this.otoparkFound = true;
console.log(this.selectedOtopark);
const toast = this.toastCtrl.create({
message: "good",
duration: 3000,
position: 'top'
});
toast.present();
} else {
this.otoparkFound = false;
const toast = this.toastCtrl.create({
message: 'something goes wrong',
duration: 3000,
position: 'top'
});
toast.present();
}
});
});
});}