Testing Ionic 2 now, and stumbled across this need:
From a Page, how to access the child components in an optimal way?
There is the app.getComponent('id')
- but it is not working within the constructor of a Page, as the child components are not ready and registered yet.
Seems the Ionic 2 Page can use the new afterViewInit
and thus this becomes possible:
...
class ParentComponent
constructor( app: IonicApp ) {
this.app = app;
// This returns 'undefined':
// this.childComponent = this.app.getComponent('childId');
}
afterViewInit() {
// This works:
this.childComponent = this.app.getComponent('childId');
this.childComponent.someMethodOnTheChild();
}
...
But: is there an easy way to fetch the child component from the parent component, without going all the way up to the app?
@ContentChildren and QueryList are an option, but unnecessarily complex.