Avatar ·

Angular 2 @ViewChild retorna undefined — como acessar corretamente o componente filho?

Olá a todos! Sou novo em Angular (2+) e TypeScript, e me deparei com um problema que está me deixando perplexo. Estou tentando acessar um componente filho através do decorator @ViewChild, mas ele sempre retorna undefined. Aqui está meu código. Tenho o componente pai `ParentComponent`: ```typescript import { Component, ViewChild, AfterViewInit } from '@angular/core'; import { ChildComponent } from './child.component'; @Component({ selector: 'app-parent', template: `
` }) export class ParentComponent { @ViewChild(ChildComponent) childComponent!: ChildComponent; callChildMethod() { if (this.childComponent) { this.childComponent.doSomething(); } else { console.log('childComponent is undefined'); } } } ``` E o componente filho `ChildComponent`: ```typescript import { Component } from '@angular/core'; @Component({ selector: 'app-child', template: `

Componente filho

` }) export class ChildComponent { doSomething() { console.log('Fazendo algo no componente filho'); } } ```