服务和组件之间的循环依赖警告(Angular)
Circular dependency warning between service and component (Angular)
假设我们有以下内容:
一个 CoolModule
(cool.module.ts) 处理很酷的东西。
CoolModule
包含以下内容:
CoolDialogComponent
(酷-dialog.component.ts)
CoolService
(cool.service.ts)
- ...其他很棒的文件
一个使用 CoolModule
的应用程序正在通过 CoolService
使用它的功能,这是一个可注入的单例服务。
CoolService
有很多很酷的方法。其中之一可以打开 material-对话框。它传递对话框的 CoolDialogComponent
以将其用作对话框的内容。
CoolDialogComponent
有一个调用 CoolService
方法之一的特定操作。
此时:
CoolDialogComponent
依赖于 CoolService
CoolService
依赖于 CoolDialogComponent
这里只有我一个人认为不应该仅仅因为循环依赖警告就重构它吗?
- 我认为
CoolService
是一种可注入服务,我们可以通过它来使用它的功能。
CoolDialogComponent
通过定义模式对话框的内容来完成它的工作。并且它能够调用它的方法。
- 它们都在
CoolModule
中被绑定在一起,它们没有彼此存在。
- 创建一个额外的服务来解决循环依赖警告似乎有点像强奸它的结构。
请解释一下我在这里没有看到的地方!
And/or提供解决警告的方法! (不,我不想在 angular-cli 的配置中将其关闭)。
The CoolDialogComponent has a particular action that calls one of the CoolService's methods.
在另一个 TypeScript 文件中定义 CoolServiceInterface
。
export interface CoolServiceInterface {
sharedMethod();
}
让服务实现接口,让组件只引用接口。
CoolService has a bunch of cool methods. One of them can open a material-dialog. It passes the CoolDialogComponent for the dialog to use it as the dialog's content.
CoolService
应该在创建模态对话框时将新的 CoolDialogContext
class 注入 CoolDialogComponent
。当您使用组件工厂创建模态对话框时,有一种方法可以在创建之前添加提供者,但这里的要点是 CoolService
应该用于 create 模态,并且 context 用于管理模态的生命周期。
假设我们有以下内容:
一个 CoolModule
(cool.module.ts) 处理很酷的东西。
CoolModule
包含以下内容:
CoolDialogComponent
(酷-dialog.component.ts)CoolService
(cool.service.ts)- ...其他很棒的文件
一个使用 CoolModule
的应用程序正在通过 CoolService
使用它的功能,这是一个可注入的单例服务。
CoolService
有很多很酷的方法。其中之一可以打开 material-对话框。它传递对话框的 CoolDialogComponent
以将其用作对话框的内容。
CoolDialogComponent
有一个调用 CoolService
方法之一的特定操作。
此时:
CoolDialogComponent
依赖于CoolService
CoolService
依赖于CoolDialogComponent
这里只有我一个人认为不应该仅仅因为循环依赖警告就重构它吗?
- 我认为
CoolService
是一种可注入服务,我们可以通过它来使用它的功能。 CoolDialogComponent
通过定义模式对话框的内容来完成它的工作。并且它能够调用它的方法。- 它们都在
CoolModule
中被绑定在一起,它们没有彼此存在。 - 创建一个额外的服务来解决循环依赖警告似乎有点像强奸它的结构。
请解释一下我在这里没有看到的地方! And/or提供解决警告的方法! (不,我不想在 angular-cli 的配置中将其关闭)。
The CoolDialogComponent has a particular action that calls one of the CoolService's methods.
在另一个 TypeScript 文件中定义 CoolServiceInterface
。
export interface CoolServiceInterface {
sharedMethod();
}
让服务实现接口,让组件只引用接口。
CoolService has a bunch of cool methods. One of them can open a material-dialog. It passes the CoolDialogComponent for the dialog to use it as the dialog's content.
CoolService
应该在创建模态对话框时将新的 CoolDialogContext
class 注入 CoolDialogComponent
。当您使用组件工厂创建模态对话框时,有一种方法可以在创建之前添加提供者,但这里的要点是 CoolService
应该用于 create 模态,并且 context 用于管理模态的生命周期。