gpt4 book ai didi

javascript - 我需要从 Angular 2 中的其他组件获取公共(public)方法

转载 作者:行者123 更新时间:2023-12-03 05:54:25 26 4
gpt4 key购买 nike

我需要从 app.component 调用 headerExpand 属性,但是当我尝试执行此操作时,请在控制台中显示此内容:

metadata_resolver.js:559Uncaught Error: Invalid providers for "Page1" - only instances of Provider and Type are allowed, got: [?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;
}
}

最佳答案

不可能将父组件注入(inject)到子组件。你应该例如在这种情况下使用Output()(子级到父级)。看看这里:

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

关于javascript - 我需要从 Angular 2 中的其他组件获取公共(public)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40000427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com