I have a Component that acts as a Search Bar. It can make api requests and provide the results to the rest of the app through an @Output
and EventEmitter
but I also want a function to go the other way. The Search Bar component keeps a history of it's searches and I would like to provide a way for the parent component to clear the history. The best I can think of is to somehow call a method in my Search Bar from a parent component.
The best I've come up with (but haven't been able to properly implement) is to try to pass an EventEmitter
INTO my Search Bar and the child subscribes to the parent's events. The @Input
hasn't completed binding by the time the constructor though so it gets tricky to actually do this.
I suppose I could try and store the history in the parent component, but the reason the Search Bar is a component is because it will appear in multiple places and this doesn't fit with Separation of Concerns so it seems best to keep the history in the Search Bar component.
How can a parent Component call a function on a child Component?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…