gpt4 book ai didi

Angular 2 应用程序结构 : Circular Module Dependencies

转载 作者:太空狗 更新时间:2023-10-29 17:57:36 24 4
gpt4 key购买 nike

在 Angular 2 风格指南中,有一个目录结构的推荐:https://angular.io/docs/ts/latest/guide/style-guide.html#04-06

我通常认为这是一个很好的建议,我打算自己做一些非常相似的事情。但是,我遇到了一个问题,我很好奇是否有人解决了它。

注意 heroes 模块包含一个 shared 目录和一个 heroes-button.component。据推测,我们希望在整个应用程序中使用此组件(因此称为“共享”)。

同样,villains 模块包含一个带有 villains-button.componentshared 目录。

如果我想在 heroes 模块的某个地方使用 villains-button.component 而在 heroes-button.component villains 模块,然后我将以循环引用结束。

简而言之:Angular 不允许我将 ModuleA 导入 ModuleB,以及将 ModuleB 导入 ModuleA,但样式指南另有说明。

有人对此场景有任何解决方案吗?

最佳答案

我的解决方案是将那些迫使我产生循环依赖的组件(在本例中为 villains-button.component 和 heroes-button.component)移动到共享模块中。

最后的目录结构是这样的:

HeroesModule
-HeroComponentA
-HeroComponentB
VillainsModule
-VillainComponentA
-VillainComponentB
SharedModule
-HeroButton
-Villain Button <-- these two are now available across the application

这可能感觉不对,因为你认为“Hero”按钮属于其他 Hero 东西,但回想起来,随着我的应用程序的增长,我很高兴 Angular 禁止模块之间的循环依赖。随着应用程序的增长,支持这种模式非常危险。

关于Angular 2 应用程序结构 : Circular Module Dependencies,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40028801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com