ionViewLoaded没有被调用 [英] ionViewLoaded not being called

查看:75
本文介绍了ionViewLoaded没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用页面生命周期事件如此处所示.我正在尝试使用ionViewDidLoad,但它没有为我解雇.我创建了一个 plunkr ,以尽可能简单的方式介绍我的示例.我在ionViewDidLoad内有一个日志语句,但是没有被解雇.如果您在plunkr中查看page1.tshome.ts,您将看到日志语句.

I'm trying to use the Page Life Cycle Events as seen here. I'm trying to use ionViewDidLoad but it's not firing for me. I created a plunkr to put my example in the most simple terms as possible. I have a log statment inside of ionViewDidLoad, but it's not being fired. If you look at page1.ts and home.ts in the plunkr you'll see the log statements.

每个文件的基本代码为:

The basic code of each file is:

import { NavController } from 'ionic-angular/index';
import { Page1 } from 'page1.ts'
import { Component } from "@angular/core";


@Component({
  templateUrl:"home.html"
})
export class HomePage {

  greeting: string;

  constructor(private nav: NavController) {

    }

    ionViewDidLoad(){
      console.log('home.ts view initialized');
  }

    goToPage1() {
      this.nav.push(Page1);
    }


}

在我自己的代码中,我每次都会调用一个API并触发它,因此我需要一个生命周期挂钩,以便我

In my own code, i'm making a call to an API and its firing everytime so I need a life cycle hook so that i

推荐答案

我认为,由于您的页面是标签页,因此生命周期事件的工作方式与常规页面不同.如果每次选择选项卡时都需要执行某项操作(例如从服务器获取数据),请检查离子文档的这部分:

I think that since your page is a tab, the lifecycle events don't work in the same way they work for regular pages. If you need to do something every time a tab is selected (like obtaining data from a server), then check this part of Ionic docs:

有时您可能想调用一个方法而不是导航到一个新方法 页. 您可以使用(ionSelect)事件在您的课程上调用方法 选择选项卡时.以下是呈现模态的示例 从其中一个标签中.

Sometimes you may want to call a method instead of navigating to a new page. You can use the (ionSelect) event to call a method on your class when the tab is selected. Below is an example of presenting a modal from one of the tabs.

<ion-tabs>
  <ion-tab (ionSelect)="getData()"></ion-tab>
</ion-tabs>

还有

export class Tabs {
  constructor(...) {

  }

  public getData() {
    // Awesome code doing awesome things...
  }
}

因此,每次选择选项卡时,您都可以使用(ionSelect)="yourMethod()"对该API进行调用.

So you can use the (ionSelect)="yourMethod()" to make the call to that API every time the tab is selected.

这篇关于ionViewLoaded没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆