繁体   English   中英

我需要从Angular 2中的其他组件获取公共方法

[英]I need get public method from other component in Angular 2

我需要从app.component调用headerExpand属性,但是当我尝试创建此属性时,请在控制台中向我展示:

metadata_resolver.js:559未捕获的错误:“ Page1”的无效提供程序-仅允许提供程序和类型的实例,得到:[?undefined?]

page1.ts

<pre>
import { Component } from '@angular/core';
import { MyApp } from '../../app/app.component';

@Component({
  selector: 'page-page1',
  templateUrl: 'page1.html',

})
export class Page1 {

  constructor(public miApp: MyApp) {
    console.log(miApp.headerExpand);

  }

</pre>

app.component.ts

<pre>
@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  @ViewChild(Nav) nav: Nav;

  rootPage: any = Page1;
  public headerExpand: boolean;
  public pages: Array<{title: string, component: any}>;

  constructor(public platform: Platform) {
    this.initializeApp();
    this.headerExpand = true;


  }

  initializeApp() {
    this.platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      StatusBar.styleDefault();
    });
  }

  openPage(page) {
    // Reset the content nav to have just this page
    // we wouldn't want the back button to show in this scenario
    this.nav.setRoot(page.component);
  }



  expandHeader(){
    this.headerExpand = false;
  }
}

无法父组件注入子组件。 您应该在这种情况下使用Output() (子代为父代)。 在这里看看:

https://angular.io/docs/ts/latest/cookbook/component-communication.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM