[英]Ionic Storage value will not display on HTML until page is reloaded
Ionic Storage values aren't displaying on HTML until page gets reloaded.在页面重新加载之前,离子存储值不会显示在 HTML 上。 I looked into some other people who had the same problems but the solutions I found either don't work or don't work anymore due to Ionic version changes.
我调查了其他一些有同样问题的人,但由于 Ionic 版本的变化,我发现的解决方案要么不起作用,要么不再起作用。 An example would be the Event system in Ionic.
一个例子是 Ionic 中的事件系统。
HTML: displays Name, and two dates HTML:显示名称和两个日期
<ion-list>
<div *ngFor="let data of data2">
<ion-item lines="none">
<ion-label text-left>
Fruit Name:
</ion-label>
<ion-label text-right>
{{data.FruitName}}
</ion-label>
</ion-item>
<ion-item lines="none">
<ion-label text-left>
Install Date:
</ion-label>
<ion-label text-right>
{{data.installTime}}
</ion-label>
</ion-item>
<ion-item>
<ion-label text-left>
Remove Date:
</ion-label>
<ion-label text-right>
{{data.removeTime}}
</ion-label>
</ion-item>
</div>
</ion-list>
.TS .TS
import {ActivatedRoute} from '@angular/router';
import { Storage } from '@ionic/storage';
export class SummaryComponent implements OnInit {
//My Variables
data2;
_idx;
constructor(
private route: ActivatedRoute,
private storage: Storage,
) { }
ngOnInit() {
this.route.params.subscribe(
params => {
/*Update Variables here*/
this._idx = Temperature.__idx;
this.storage.ready().then(() => {
this.storage.get(this._idx).then(data=>{
//if data exists
if(data)
{
console.log("data read");
this.data2 = data;
console.log(data);
}
else
{
console.log("data is empty");
this.data2 = [];
}
});
});
});
}
}
When I load the routing page for the first time当我第一次加载路由页面时
When I change routing page then go back to the original.当我更改路由页面时,请返回原始页面。
In your .ts file do this,在您的 .ts 文件中执行此操作,
import {
ActivatedRoute
} from '@angular/router';
import {
Storage
} from '@ionic/storage';
import {
ChangeDetectorRef
} from '@angular/core';
export class SummaryComponent implements OnInit, OnDestroy {
//My Variables
routeParams;
data2;
_idx;
constructor(
private route: ActivatedRoute,
private storage: Storage,
private changeRef: ChangeDectetorRef
) {}
ngOnInit() {
this.routePrams = this.route.params.subscribe(params => /** ... do whatever you need to do in here that involves params **/);
this._idx = Temperature.__idx;
this.storage.get(this._idx).then(data=>{
//if data exists
if(data)
{
console.log("data read");
this.data2 = data;
console.log(data);
}
else
{
console.log("data is empty");
this.data2 = [];
}
this.changeRef.detectChanges();
});
}
ngOnDestroy() {
this.routeParams.unsubscribe();
}
}
Explanation: I don't know why you're using storage.ready()
it is not needed.说明:我不知道你为什么使用
storage.ready()
它是不需要的。 I don't know why you're making a storage call inside an observable that is never used in the storage call;我不知道你为什么要在一个从未在存储调用中使用的可观察对象中进行存储调用; separate them (unless you are using the param variable).
将它们分开(除非您使用 param 变量)。 You need to unsubscribe from every subscription you make;
您需要取消订阅您所做的每个订阅; do so otherwise you risk client side memory leaks .
这样做,否则您将面临客户端内存泄漏的风险。
In your template .html do this,在您的模板 .html 中执行此操作,
<ion-list *ngIf="data2">
<div *ngFor="let data of data2">
<ion-item lines="none">
<ion-label text-left>
Fruit Name:
</ion-label>
<ion-label text-right>
{{data.FruitName}}
</ion-label>
</ion-item>
<ion-item lines="none">
<ion-label text-left>
Install Date:
</ion-label>
<ion-label text-right>
{{data.installTime}}
</ion-label>
</ion-item>
<ion-item>
<ion-label text-left>
Remove Date:
</ion-label>
<ion-label text-right>
{{data.removeTime}}
</ion-label>
</ion-item>
</div>
</ion-list>
Explanation: Since your storage result is a promise-like object you need to wait for it to resolve.说明:由于您的存储结果是一个类似承诺的对象,您需要等待它解析。 You can do that by doing a template check on the status of the variable
data2
that you're trying to set via storage call.您可以通过对您尝试通过存储调用设置的变量
data2
的状态进行模板检查来做到这一点。 It is initially unset but after the storage call resolves it is set.它最初未设置,但在存储调用解析后设置。 This resolution might happen outside of the angular lifecycle hooks, hence why you're seeing the page initially blank and then when you reload it works fine.
此解决方案可能发生在 angular 生命周期挂钩之外,因此为什么您看到页面最初是空白的,然后当您重新加载时它工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.