2017-04-19 7 views
0

컴포넌트로부터 함수 호출을 원합니다. app.component.html각도 2 컴포넌트 함수 호출

<div class="side"> 
<app-menu></app-menu> 
</div> 
<div class="page"> 
    <router-outlet></router-outlet> 
</div> 

어떻게 기능에 '<app-menu></app-menu>'을 호출하는

import { Component, Input } from '@angular/core'; 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 

export class AppComponent { 
    title = 'app works!'; 

} 

을 app.component.ts?

응용 프로그램 메뉴 (menu.component.ts)

import { Component } from '@angular/core'; 

@Component({ 
    ... 
}) 
export class MenuComponent { 
    addItem(index: number, heading: string, route: string): void { 
    } 
    groups: Array<any> = []; 
} 

답변

1

당신이 달성하려고 무엇 확실하지. 내 생각 엔

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 

export class AppComponent { 
    @ViewChild(MenuComponent) menu:MenuComponent; 

    title = 'app works!'; 

    // can't be called before the `ngAfterViewInit()` lifecycle callback. 
    foo() { 
    this.menu.addItem(1, "heading foo", "some route"); 
    } 
} 
+0

은 U 감사! :) LOLLLLLLL –

관련 문제