首页 文章

Angular:从延迟加载模块导出组件

提问于
浏览
0
  • AppModule

将ShellModule作为延迟加载

  • ShellModule

import SharableModule使用SharableComponent将FooModule作为延迟加载

SharableModule

export SharableComponent提供SharableService

  • FooModule

路由FooComponent或BarComponent

  • FooComponentBarComponent

使用ShareableComponent


使用延迟加载不起作用 SharableModule.exports 所以不能在 FooModule 中使用 SharableComponent

是吗?

我将 SharableModule 的组件列表添加到 FooModule 然后正常工作 .

但发生错误,因为在2个模块上声明( ShellModuleFooModule ) .

ShellModule 所需组件 SharableModule 功能如 FooModule

有什么正确的方法吗?

2 回答

  • 0

    不要使用SharableModule来提供SharableService,因为导入服务的每个延迟加载模块将以不同的服务实例结束 . 如果您需要将所有这些服务用作单例,则将它们添加到核心模块 . 您还可以创建单独的core-service.module . 使用可共享模块只共享组件或指令 .

    你可以这样做

    core-components.module.ts[ Add core components]
    core-services.module.ts[ Add all shared services]
    shared.module.ts [ this module exports all shared component and modules]
    

    在shell.module.ts中导入SharedModule,即shared.module在foo.module中导入SharedModule,即shared.module同样,你可以添加尽可能多的模块[延迟加载或非延迟加载],只需将SharedModule作为公共模块导入 .

    这将防止任何循环依赖,并允许您跨应用程序模块共享公共组件 .

  • 0

    它在docs得到了相当全面的解决 .

    通常,您应该__98593_,您可以导入/导出所有可共享的组件/指令,其他模块 . 它将在您想要使用它的任何延迟加载模块中导入 .

    对于 services ,通常它们应该是单个(单个实例)供您的整个应用程序使用 . 因此,它们应该在所谓的 core.module 中进行分组,并且此模块将导入到 root.module (aka.app.module`)中 .

    任何 feature modules 应该是延迟加载模块 .

    项目结构是这样的:

    +--app
        |
        +--core.module/
        |   |
        |   +--(import your sevices here...)
        |
        +--shared.module
        |   |
        |   +--(import/export all your components/directives/modules here)
        |
        +--any-lazy-loading.module
        |   |
        |   +--(import shared-module here)
        |
        app.module
            |
            +--(import core.module here)
    

相关问题