我需要从Angular 2中的其他组件中获取公共方法
I need get public method from other component in Angular 2
我需要从app.component调用headerExpand属性,但当我尝试这样做时,请在控制台中显示:
metadata_resolver.js:559未捕获错误:无效的"Page1"提供程序-只允许提供程序和类型的实例,得到:[?)
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相关文章:
- 如何在另一个组件中获取指令/组件实例
- Vue.js获取组件中的一个元素
- 从子级获取React dom组件
- React - 从 DOM 元素获取组件以进行调试
- 如何获取子组件的内容Angular 2中的子组件
- 在 ExtJS 中获取父组件
- 如何创建一个组件,我可以设置在 Angular 2 中的属性中获取 URL 数据
- 从子窗体组件ReactJS中获取道具
- 如何在Vue.js中获取组件元素的offsetHeight
- 如果我知道 DOM 元素的 ID,如何获取组件
- 获取组件的实际宽度和高度
- 在渲染方法 ReactJS 中获取组件宽度
- 如何使用javascript获取组件的容器?-HTML
- 如何在react native中获取组件的大小
- Ember模型和组件,获取组件来检测模型中的更改
- 如何在不向其父元素/子元素添加逻辑的情况下获取组件中子元素的dom节点
- 如何从 KnockoutJS 中的组件节点获取组件的视图模型
- 如何在Angular 1.5和Typescript中获取组件控制器中的绑定值?
- CQ5 AEM:如何使用javascript在对话框中按名称获取组件
- 获取组件的Drop Target