【问题标题】:Resolve Angular circular module dependencies and components(parent-child)解决 Angular 循环模块依赖和组件(父子)
【发布时间】:2021-08-13 04:20:45
【问题描述】:

我有以下结构

Project
---Module1
------ComponentA
------componentB
---Module2
------componentC
------componentD

Module1.module.ts 的导入中,我正在导入Module2,因为在componentA 中我使用的componentC 属于Module2

现在,我需要在我的 componentD 中使用 componentB(Module1) 属于 module2 但是我有很多错误,因为当我在我的 Module2.module.ts 中导入 Module1 时创建了循环依赖项

此时的结构是:Module1Module2 的父级。所以,我不知道是否存在某种方式可以从Module2(Child) 中的一个组件访问属于Module1(parent) 的一个组件

【问题讨论】:

  • 我的回答是重新考虑组件 B 是否应该是模块 1 的子模块。如果模块 2 中需要它并且模块 2 是模块 1 的子模块,则它不属于那里。循环依赖揭示了您设计中的问题,并明确说明何时需要重新考虑。

标签: angular typescript module parent-child circular-dependency


【解决方案1】:

如果您不关心当前的模块结构,您可以为组件 B 和 C 创建第三个模块 - 您可以将 module 3 导入到 module 1module 2

Project
---Module1
------ComponentA
---Module2
------componentD
---Module3
------componentC
------componentB

【讨论】:

    猜你喜欢
    • 2016-08-27
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 2012-03-09
    • 2020-11-12
    相关资源
    最近更新 更多