服务和组件之间的循环依赖警告(Angular)

Circular dependency warning between service and component (Angular)

假设我们有以下内容: 一个 CoolModule (cool.module.ts) 处理很酷的东西。 CoolModule 包含以下内容:

一个使用 CoolModule 的应用程序正在通过 CoolService 使用它的功能,这是一个可注入的单例服务。

CoolService 有很多很酷的方法。其中之一可以打开 material-对话框。它传递对话框的 CoolDialogComponent 以将其用作对话框的内容。

CoolDialogComponent 有一个调用 CoolService 方法之一的特定操作。

此时:

这里只有我一个人认为不应该仅仅因为循环依赖警告就重构它吗?

请解释一下我在这里没有看到的地方! 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 用于管理模态的生命周期。