2017-04-19 20 views
0

コンポーネントからの関数呼び出しが必要です。角2コンポーネント関数呼び出し

app.component.html

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

app.component.ts機能で '<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!'; 

} 

アプリメニュー(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

ありがとう! :) LOLLLLLLL –

関連する問題