I have an Angular component called "tree". What I want is to call methods on tree from any other component in my app. There will only be one tree in my entire app at any time. Is it ok to create the component in the place I want to view it and share the componentRef using a service this way:
export class AppComponent implements OnInit {
@ViewChild("tree") treeComponent: TreeComponent;
constructor(private treeService: TreeService) { }
ngOnInit() {
this.treeService.setTreeReference(this.treeComponent);
}
}
Then I could call it this way:
export class OtherComponent {
constructor(private treeService: TreeService) { }
getActiveNode() {
return this.treeService.treeReference.getActiveNode();
}
}
Is this a good practice in Angular2+ or generally in MVC?
Edit1: I forgot to specify that I don't have access to the TreeComponent code as it is imported from a library.