共享单例服务并避免共享组件中的循环依赖 [英] Sharing Singleton Services And Avoiding Circular Dependencies in Shared Components

查看:31
本文介绍了共享单例服务并避免共享组件中的循环依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该项目使用的是在本地NPM服务器上运行的Angular 2和TypeScript,并且是使用Angular CLI构建的.

This project is using Angular 2 and TypeScript running on a local NPM server and was built using the Angular CLI.

该项目共有三个模块.第一个用于非ui服务和组件.第二个是针对UI组件和布局的.第三个也是最后一个模块是针对UI服务的.如下图所示,它们已分解:

This project has three modules in total. The first one is for non-ui services and components. The second one is for UI components and layouts. And the third and final module is for UI services. They are broken down as seen in the image below:

如您所见,我真的不确定在这里是否需要父模块,因此最顶层的项目是?PARENT MODULE?".

As you can see, I'm really not sure whether I need a parent module here or not, hence the top-most item "? PARENT MODULE?"

  1. 在多个模块中共享单项服务
  2. 当两个模块彼此需要运行时,避免循环依赖

问题1-跨多个模块共享服务单例

就我而言,我需要 UI模块非UI模块,以共享 UI服务模块中所有服务的单例strong>.

Problem 1 - Sharing a Service Singleton Across Multiple Modules

In my case, I need both the UI module and non-UI module to share singletons of all of the services in the UI services module.

到目前为止,我已经尝试使用 forRoot()无济于事,并尝试将服务本身移动到父模块并从那里提供它们.无论我做什么,似乎都无法从UI服务模块中获得两个模块来共享一个单例.

So far I have tried using forRoot() to no avail, as well as attempting to move the services themselves to a parent module and provide them from there. No matter what I do, I cannot seem to get both of the modules to share a singleton from the UI services module.

我的应用程序中面临的另一个问题是,我有一个模块的一部分,而该模块需要所有其他模块,而这又需要第一个模块的一部分才能正常工作.

The other problem I am facing in my application is that I have a part of one module that needs all of another module, which in turns needs the part of the first module in order to work.

在非UI模块中,您将看到页眉/页脚需要使用UI模块才能起作用.但是,在UI模块中,布局需要访问页眉/页脚,以便将这些项目作为布局的一部分提供.这形成了循环依赖,我无法完全消除.

In the non-UI module, you will see that the header/footer need to consume the UI module in order to function. However, in the UI module, the layouts need access to the header/footer in order to provide those items as part of the layout. This forms a circular dependency that I cannot quite wrap my head around eliminating.

我曾考虑过将布局从UI模块移到其自己的模块中,但是它们也需要引用UI模块的组件部分,因此,一旦我导入该模块,便会在整个环境中创建循环依赖项再次.

I thought about moving the layouts out of the UI module into its own module, however they too need a reference to the components portion of the UI module, so if as soon as I import that module I create the circular dependency all over again.

我需要一种消除循环依赖的方法,并以某种方式在所有其他模块之间共享UI服务模块中所有服务的单例.

I need a way to eliminate the circular dependency, and somehow share a singleton of all of the services in the UI services module between all other modules.

我已经连续一周尝试寻找这两个问题的解决方案,并且我认为在这一点上我看不到树木茂密的森林.我确定我缺少一些愚蠢的东西,但是我似乎无法弄清楚这个问题.

I have been trying to find a solution to these two problems for a week straight now, and I think at this point I can't see the forest for the trees. I'm sure I'm missing something stupid, but I just can't seem to figure this problem out.

推荐答案

具有一个 CoreModule ,它仅提供不依赖于任何其他模块的服务,并且应该是单例.此模块应仅由根 AppModule 导入.这将确保这些服务是单例(仅在 CoreModule 中提供).在我的应用程序中,这是我提供诸如 AuthService 之类的全局服务的地方.这样可以解决您的单例问题.

Have a CoreModule that provides only services that do not depend on any other module, and that are expected to be singletons. This module should be imported by the root AppModule only. This will guarantee that those services are singletons (they are only provided in the CoreModule). In my app this is where I have global services such as AuthService. This will solve your singleton problem.

具有一个 SharedModule ,它可以导出由多个模块共享的组件,指令甚至Angular模块.在我自己的应用程序中,该模块包括我使用过的所有组件,例如 DialogComponent 或Angular自己的 FormsModule CommonModule .此 SharedModule 不提供您的全局服务.将此模块导入其他所有模块.这将解决您的循环依赖问题.

Have a SharedModule that exports components, directives and even Angular Modules that are shared by multiple modules. In my own app, this module includes components that I use all over, such as DialogComponent, or Angular's own FormsModule and CommonModule. This SharedModule does not provide your global services. Import this module into every other module. This will solve your circular dependencies problem.

简而言之,您可以将 CoreModule SharedModule 视为帮助程序模块.它们不会单独构建您的站点,但是会提供您的站点模块所需的依赖项.您可能会受益于阅读文档的本章,然后回到您的项目.

In short, you can think of CoreModule and SharedModule as helper modules. On their own they don't build out your site, but they provide the dependencies needed by your site modules. You may benefit from reading this chapter of the docs, and then getting back to your project.

这篇关于共享单例服务并避免共享组件中的循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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