Angular 爱奥尼亚3上的locastorage和tabs菜单
我在表1上有一张地图 我单击标记并发送localstorage标记标题并发送到tab2。那很好Angular 爱奥尼亚3上的locastorage和tabs菜单,angular,typescript,local-storage,ionic3,Angular,Typescript,Local Storage,Ionic3,我在表1上有一张地图 我单击标记并发送localstorage标记标题并发送到tab2。那很好 that.passingData(dogwalkMarker.valueOf().parkid); passingData(something: string) { localStorage.setItem('passingData', JSON.stringify(something)); console.log(something); 但是当我第一次在return之后加载tab2时,出现了一些
that.passingData(dogwalkMarker.valueOf().parkid);
passingData(something: string) {
localStorage.setItem('passingData', JSON.stringify(something));
console.log(something);
但是当我第一次在return之后加载tab2时,出现了一些问题。在我加载应用程序上的任何页面并加载tab 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 = {};
}
您的本地存储似乎存在
async
问题。因此,我强烈建议使用。由于它支持开箱即用的承诺,您可以轻松地在async
情况下使用它。如果您也安装了SQLite,它可以在所有平台上正常工作(尤其是iOS
设备,这是必需的)
文档中的示例:
更新:
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 = {};
}
});
});
在首次加载时
需要使用:
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 = {};
}
});
});
解决方案 这是我的错,因为我定义了在构造函数中获取数据库代码。因为该页面需要加载以获取数据 被遗忘的代码部分
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();
}
});
});
});}
你好@我试过这个。但同样的问题还是出了问题<代码>this.storageCtrl.get('passingData')。然后((data)=>{this.x=data;console.log(data);})代码>查看更新。我将所有代码复制到
this.platform.ready()中,然后(()=>{})代码>但不起作用。能否显示上次代码更改?把它放在你的问题下面。你在哪里使用这个this.x=data代码>?