简体   繁体   English

在页面重新加载之前,Ionic Storage 值不会显示在 HTML 上

[英]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.

相关问题 在重新加载页面IONIC之前,登录表单不起作用 - Login form not working until the page is being reloaded IONIC 在重新加载页面之前,Ionic Storage get不起作用 - Ionic Storage get doesn't work until reloading page 从离子存储中检索数据后,有没有办法让 html 页面加载? - Is there a way for html page to loads after retrieving data from ionic storage? 在重新加载页面之前,ngrx存储不会在其他路由上更新 - ngrx store not updated on another route until page is reloaded 如何在Ionic With Angular中基于本地存储值加载页面 - How to load a Page based on local storage value, in Ionic With Angular 如何使用离子存储(离子3,Angular 5,科尔多瓦)在HTML上显示存储的项目 - How to display stored items on HTML using Ionic storage (Ionic 3, Angular 5, Cordova) 更新本地存储中的值并显示在网页上,无需刷新页面 - Update the value in local storage and display it on web page without refreshing the page Angular 路由,是整个 html 页面重新加载,还是页面的一部分? - Angular routing, is the entire html page reloaded, or just a portion of the page? ionic 4以Html显示数据存储 - ionic 4 Show data storage in Html Ionic 组件的 html 不显示 - Ionic The html of a component is not display
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM