问题
I would like to have some rules or suggestions to be able to do this:
I have an alert on app level that I want to show when something is happening on one of my components (ex: Saving information has succeeded or failed).
Also, how can I send the different text I want to show? Is there any equivalent of @ViewChild?
Thanks for your help
回答1:
I would do it with Observable
and Subject
. Basically you need a service
injected in the shared module of those component so they share state.
The app (root) component will have a subscription
on the public observable
of the service. And every component will set the state through that service
by calling the .next()
on the subject
. The observable
will return the value of the subject.
You can check the code here: https://angular.io/guide/component-interaction#parent-and-children-communicate-via-a-service
The idea is quite similar.
来源:https://stackoverflow.com/questions/47440926/how-to-display-alert-on-app-level-from-a-child-component