angular7 - 在 Ionic 4、Angular 7 中重新加载当前页面

标签 angular7 ionic4

我正在尝试为那些在没有互联网连接的情况下运行应用程序的人建立后备。 在 mypage.ts 中,我有以下内容:

import { Component, OnInit } from '@angular/core';
import { Network } from '@ionic-native/network/ngx';
@Component({
  selector: 'app-contacto',
  templateUrl: './contacto.page.html',
  styleUrls: ['./contacto.page.scss'],
})
export class ContactoPage implements OnInit {
  constructor(private network: Network) {
        // watch network for a connection
        this.network.onConnect().subscribe(() => {
          document.getElementById("netOFF").style.display="none";
         // document.getElementById("netRel").style.display="none";
          console.log('Internet ON');
        });
         // watch network for a disconnection
         this.network.onDisconnect().subscribe(() => {
          document.getElementById("netON").style.display="none";
          console.log('Sin conexión a Internet');
        });
        function reload() {
          this.ionViewWillEnter();
      }
      }
  ngOnInit() { }
}

我的 html 文件如下所示:

<ion-content>
    <div id="netON">
        <iframe src="https://www.domain.es/" style="top:57px; left:0; bottom:0; right:0; width:100%; height:100%; border:none; margin:0; padding: 0; overflow:hidden; z-index:999999;"></iframe>
      </div>
<div id="netOFF"><ion-img src="/assets/img/net.png" style="display:block; width:100%; height:100%; object-fit: cover; z-index:999999;"></ion-img>
</div>
  </ion-content>
  <ion-footer id="netRel" (click)="reload()">
      <ion-toolbar color="danger">
          <ion-icon name="exit" slot="start"></ion-icon>
        <ion-title>Reintentar</ion-title>
      </ion-toolbar>
    </ion-footer>

所以,联系表单是一个 iframe,如果手机没有互联网连接,netON(iFrame)将被隐藏,如果手机有互联网连接,则 iframe将显示。

此外,我还有一个按钮(click)="reload()",供那些没有连接且返回在线状态以便重新加载页面的用户使用。

我需要一种在用户点击 Ionic 4 时重新加载页面的方法。

Please note that i made some changes in order to test the button action on click so i made it visible, but it's hidden if no Internet, and the picture as well.

最佳答案

您的代码没有显示您在调用 reload() 方法时实际加载的数据。 **注意:您的 reload() 函数不应位于构造函数中。它应该在与 ngOnInit() { } **

相同级别的构造函数之外

因此,如果有数据需要获取,请像这样调用 reload() 方法中的函数/方法来重新获取并更新 View :

reload() {
    this.getData();
}

// where getData() method is what does the actually loading of the data like:

getData() {
    this.apiService.fetchData()
    .subscribe(
        data => {
            this.items = data;
    }, err => {});
}

关于angular7 - 在 Ionic 4、Angular 7 中重新加载当前页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55601311/

相关文章:

ionic-framework - MinDate 和 MaxDate 不适用于 Ionic 4 中的 native 日期选择器

javascript - 响应式(Reactive)表单还是模板表单?

angular - 当应用程序在外部服务器上运行时,所有请求均未找到(404)

javascript - 如何获取经过身份验证的用户的数据/id?

android - 命令失败,并在IONIC 4上显示退出代码

html - 动态更改元素 css 属性

angular - 尝试设置路径时无法读取未定义的属性 'loadChildren' 中的错误

angular - 如何在 Angular 7 中从 ckeditor 5 检索数据?

node.js - 创建新的 Angular 7 项目时出现问题

angular-ui-router - ionic 4 : How to set navigation direction (backward/forward) using routerLink?