2017-05-09 127 views
0

我在我的離子應用程序中有一個全球頁腳,我這樣做的方式就是這樣。我在ion-nav中添加了選擇器組件,它運行良好。我的頁腳顯示在每一頁中。角2離子2,調用子組件undefined

app.html

<ion-nav [root]="rootPage" #content swipeBackEnabled="false"> 

</ion-nav> 
<call-footer #CallChild></call-footer> 

然而,在一個網頁,我想有頁腳的控制,我可以隱藏或更改裏面的變量。在我的頁腳組件中,我有一個名爲'test'的函數,並且在其中一個頁面('調用')中我正在嘗試這個。

calls.ts

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

import { oncallpage } from '../calls/oncall/oncall'; 
import { callfooter } from '../../components/callfooter/callfooter.component'; 

@Component({ 
    selector: 'page-calls', 
    templateUrl: 'calls.html' 
}) 
export class callspage implements OnInit { 

    @ViewChild('CallChild') child; 


    constructor() { } 


     ngAfterViewInit() { 
     this.child.test(); //error 
    } 

但是我得到「無法讀取屬性‘測試’的未定義」

我不知道是否有語法錯誤或我我錯過了一些想法。我該如何解決這個案子?

+0

你的c alls.html的樣子? – echonax

+0

我編輯過。這是一個calls.html –

+0

我沒有看到calls.html。有一個app.html – echonax

回答

4

@ViewChild返回ElementRef。你不能調用這樣的函數。

一種解決方法是使用離子的Events(docs)

的功能更改爲:

ngAfterViewInit() { 
    this.events.publish("child:test"); 
} 

而在你的子頁:

constructor(events: Events) { 
    events.subscribe("child:test",()=> { 
    this.test(); 
    }); 
} 

test() { console.log("test called"); } 

也爲更詳細的解釋,請參考我的答案在這裏How to update value inside ionic 2 side menu

+0

只是試了一下,它的工作!謝謝! –

+0

嗨,跟進問題。我想這樣的事情 // callspage this.events.publish(「child:initializefooter」,this); //頁腳 events.subscribe( 「孩子:initializefooter」(頁)=> { this.ModalExitCall =頁; this.ModalExitCall.present(); //打開模型得到錯誤: //沒有爲[object Object]找到組件工廠 }) 有沒有辦法將輸入參數轉換爲組件? –

+0

在我的手機上,但看看Angular的'output'參數。你可以用它們來調用一個函數 – Ivaro18