I am new to angular2
I have a button in parent component, if
After i searched a lot about this issue, i found a solution and wish it could help you. contentChild/ViewChild have no access to component(and it's data) that is loaded in router-outlet, and using sharedService needs an extra file and more code.
You just need to read component that is loaded in the router outlet:
Add template reference to your router outlet:
Import the RouterOutlet from @angular/router :
import {...
RouterOutlet,
..} from "@angular/router";
And finally, use it:
@ViewChild("myRouterOutlet", { static: true }) routerOutlet: RouterOutlet;
buttonClicked(){
const childComp = this.routerOutlet.component as childComponent;
childComp.childFunction();
}
Hope it could help you :)