如何通过子组件在应用程序级别上显示警报 [英] How to display alert on app level from a child component

查看:82
本文介绍了如何通过子组件在应用程序级别上显示警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有一些规则或建议可以做到这一点:

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).

此外,如何发送要显示的其他文字?有@ViewChild的等效项吗?

Also, how can I send the different text I want to show? Is there any equivalent of @ViewChild?

感谢您的帮助

推荐答案

我会用ObservableSubject做到这一点.基本上,您需要在这些组件的共享模块中注入service,以便它们共享状态.

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.

应用程序(根)组件在服务的public observable上将具有subscription.每个组件都将通过调用subject上的.next()来通过该service设置状态. observable将返回subject.

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.

您可以在此处检查代码: https://angular.io/guide/component-interaction#parent-and-children-communicate-via-a-service

You can check the code here: https://angular.io/guide/component-interaction#parent-and-children-communicate-via-a-service

这个想法很相似.

这篇关于如何通过子组件在应用程序级别上显示警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆