Ionic 2 - ViewChild component is undefined
<p><strong>THE SITUATION</strong>:</p>

<p>From the app component I need to call a method in another component.</p>

<p>I read that <code>@ViewChild</code> is the way to do it.</p>

<p>But is not working in my case. I am getting the following error:</p>

<pre><code>Cannot read property ... of undefined
</code></pre>

<p><strong>THE CODE</strong>:</p>

<p>This for example is a simple test method inside the <strong>HomePage component</strong>:</p>

<pre><code>testChild()
{
    alert('child working');
}
</code></pre>

<p>In the <strong>app.component</strong> I declare HomePage as the child component: </p>

<pre><code>@ViewChild(HomePage) homePage: HomePage;
</code></pre>

<p>and then call the method from the constructor:</p>

<pre><code>this.homePage.testChild();
</code></pre>

<p>It should work right?</p>

<p>Instead I am getting this error:</p>

<p><a href="https://i.stack.imgur.com/EL78L.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EL78L.png" alt="enter image description here"></a></p>

<p>The problem is not that the view is not loaded yet.</p>

<p>I have tried also to call the child from a click event and got the same error.</p>

<p><strong>THE QUESTION</strong>:</p>

<p>Why is the child component is undefined?</p>

<p>Do you know what am I doing wrong?</p>

<p>Thanks!</p>