且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Angular中创建发布者/订阅者模型?

更新时间:2022-06-24 22:02:45

Angular 2附带了一个名为RxJs的可观察性库,因此可用于创建我们可以订阅和提交事件的服务.

With Angular 2 comes an observables library called RxJs, so that can be used to create a service that we can subscribe to and submit events.

然后,我们使用依赖项注入机制将服务注入到我们需要的应用程序中的任何位置.

Then we use the dependency injection mechanism to inject that service anywhere on the application where we need it.

因此,不再需要广播/发射事件机制,因为它已被功能更强大的Observables机制所取代,该机制内置在框架的各处:EventEmitter是可观察的,表单值是Observable,http可以返回观测值等.

So there is no need anymore for the broadcast/emit event mechanism as it was replaced by the more powerful observables mechanism, which is built-in everywhere in the framework: EventEmitter is an observable, form values are observables, http can return observables etc.

有关如何使用RxJ进行发布以构建服务的示例,请参见存储库/订阅.

See this repository for examples of how to build services using RxJs for publish/subscribe.