How to display alert on app level from a child component

纵然是瞬间 提交于 2019-12-04 02:42:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!