ionic2 - 调用父或根组件函数

时间:2016-11-18 03:27:21

标签: angular ionic2

我正在使用ionic2 sidemenu模板来构建我的应用程序:

app.component.ts

export class MyApp {
  @ViewChild(Nav) nav: Nav;

  rootPage: any = Home;
  ...
  public callFromChild(){///<-- called by child component home.ts
    console.log("called from child component");
  }

我希望在callFromChild()child component

状态下从push() {strong} {<1}}调用

home.ts

pop()

我得到了错误:

  

error_handler.js:47 EXCEPTION:./ Home class Home - inline出错   模板:28:45引起:无法设置属性'callFromChild()'为null

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您收到此错误是因为this.navCtrl.parent解析为null,因此您尝试在空值上调用属性。您是否正确包含navCtrl

// import navCtrl for use in component
constructor(public navCtrl: NavController) {}