填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

特性模块的分类

Types of feature modules

下面是特性模块的五个常用分类,包括五组:

There are five general categories of feature modules which tend to fall into the following groups:

  • 领域特性模块。

    Domain feature modules.

  • 带路由的特性模块。

    Routed feature modules.

  • 路由模块。

    Routing modules.

  • 服务特性模块

    Service feature modules.

  • 可视部件特性模块。

    Widget feature modules.

虽然下面的指南中描述了每种类型的使用及其典型特征,但在实际的应用中,你还可能看到它们的混合体。

While the following guidelines describe the use of each type and their typical characteristics, in real world apps, you may see hybrids.

特性模块

Feature Module

指导原则

Guidelines

领域

Domain

领域特性模块用来给用户提供应用程序领域中特有的用户体验,比如编辑客户信息或下订单等。

Domain feature modules deliver a user experience dedicated to a particular application domain like editing a customer or placing an order.

它们通常会有一个顶层组件来充当该特性的根组件,并且通常是私有的。用来支持它的各级子组件。

They typically have a top component that acts as the feature root and private, supporting sub-components descend from it.

领域特性模块大部分由 declarations 组成,只有顶层组件会被导出。

Domain feature modules consist mostly of declarations. Only the top component is exported.

领域特性模块很少会有服务提供者。如果有,那么这些服务的生命周期必须和该模块的生命周期完全相同。

Domain feature modules rarely have providers. When they do, the lifetime of the provided services should be the same as the lifetime of the module.

领域特性模块通常会由更高一级的特性模块导入且只导入一次。

Domain feature modules are typically imported exactly once by a larger feature module.

对于缺少路由的小型应用,它们可能只会被根模块 AppModule 导入一次。

They might be imported by the root AppModule of a small application that lacks routing.

路由

Routed

带路由的特性模块是一种特殊的领域特性模块,但它的顶层组件会作为路由导航时的目标组件。

Routed feature modules are domain feature modules whose top components are the targets of router navigation routes.

根据这个定义,所有惰性加载的模块都是路由特性模块。

All lazy-loaded modules are routed feature modules by definition.

带路由的特性模块不会导出任何东西,因为它们的组件永远不会出现在外部组件的模板中。

Routed feature modules don’t export anything because their components never appear in the template of an external component.

惰性加载的路由特性模块不应该被任何模块导入。如果那样做就会导致它被急性加载,破坏了惰性加载的设计用途。 也就是说你应该永远不会看到它们在 AppModuleimports 中被引用。 急性加载的路由特性模块必须被其它模块导入,以便编译器能了解它所包含的组件。

A lazy-loaded routed feature module should not be imported by any module. Doing so would trigger an eager load, defeating the purpose of lazy loading.That means you won’t see them mentioned among the AppModule imports. An eager loaded routed feature module must be imported by another module so that the compiler learns about its components.

路由特性模块很少会有服务提供者,原因参见惰性加载的特性模块中的解释。如果那样做,那么它所提供的服务的生命周期必须与该模块的生命周期完全相同。不要在路由特性模块或被路由特性模块所导入的模块中提供全应用级的单例服务。

Routed feature modules rarely have providers for reasons explained in Lazy Loading Feature Modules. When they do, the lifetime of the provided services should be the same as the lifetime of the module. Don't provide application-wide singleton services in a routed feature module or in a module that the routed module imports.

路由

Routing

路由模块为其它模块提供路由配置,并且把路由这个关注点从它的配套模块中分离出来。

A routing module provides routing configuration for another module and separates routing concerns from its companion module.

路由模块通常会做这些:

A routing module typically does the following:

  • 定义路由。

    Defines routes.

  • 把路由配置添加到该模块的 imports 中。

    Adds router configuration to the module's imports.

  • 把路由守卫和解析器的服务提供者添加到该模块的 providers 中。

    Adds guard and resolver service providers to the module's providers.

  • 路由模块应该与其配套模块同名,但是加上“Routing”后缀。比如,foo.module.ts 中的 FooModule 就有一个位于 foo-routing.module.ts 文件中的 FooRoutingModule 路由模块。 如果其配套模块是根模块 AppModuleAppRoutingModule 就要使用 RouterModule.forRoot(routes) 来把路由器配置添加到它的 imports 中。 所有其它路由模块都是子模块,要使用 RouterModule.forChild(routes)

    The name of the routing module should parallel the name of its companion module, using the suffix "Routing". For example, FooModule in foo.module.ts has a routing module named FooRoutingModule in foo-routing.module.ts. If the companion module is the root AppModule, the AppRoutingModule adds router configuration to its imports with RouterModule.forRoot(routes). All other routing modules are children that import RouterModule.forChild(routes).

  • 按照惯例,路由模块会重新导出这个 RouterModule,以便其配套模块中的组件可以访问路由器指令,比如 RouterLinkRouterOutlet

    A routing module re-exports the RouterModule as a convenience so that components of the companion module have access to router directives such as RouterLink and RouterOutlet.

  • 路由模块没有自己的可声明对象。组件、指令和管道都是特性模块的职责,而不是路由模块的。

    A routing module does not have its own declarations. Components, directives, and pipes are the responsibility of the feature module, not the routing module.

路由模块只应该被它的配套模块导入。

A routing module should only be imported by its companion module.

服务

Service

服务模块提供了一些工具服务,比如数据访问和消息。理论上,它们应该是完全由服务提供者组成的,不应该有可声明对象。Angular 的 HttpClientModule 就是一个服务模块的好例子。

Service modules provide utility services such as data access and messaging. Ideally, they consist entirely of providers and have no declarations. Angular's HttpClientModule is a good example of a service module.

根模块 AppModule 是唯一的可以导入服务模块的模块。

The root AppModule is the only module that should import service modules.

窗口部件

Widget

窗口部件模块为外部模块提供组件、指令和管道。很多第三方 UI 组件库都是窗口部件模块。

A widget module makes components, directives, and pipes available to external modules. Many third-party UI component libraries are widget modules.

窗口部件模块应该完全由可声明对象组成,它们中的大部分都应该被导出。

A widget module should consist entirely of declarations, most of them exported.

窗口部件模块很少会有服务提供者。

A widget module should rarely have providers.

如果任何模块的组件模板中需要用到这些窗口部件,就请导入相应的窗口部件模块。

Import widget modules in any module whose component templates need the widgets.

下表中汇总了各种特性模块类型的关键特征。

The following table summarizes the key characteristics of each feature module group.

特性模块

Feature Module

声明 declarations

Declarations

提供者 providers

Providers

导出什么

Exports

被谁导入

Imported by

领域

Domain

Yes

罕见

Rare

顶层组件

Top component

特性模块,AppModule

Feature, AppModule

路由

Routed

Yes

罕见

Rare

No

None

路由

Routing

No

有(守卫)

Yes (Guards)

RouterModule

特性(供路由使用)

Feature (for routing)

服务

Service

No

Yes

No

AppModule

窗口部件

Widget

Yes

罕见

Rare

Yes

特性

Feature


关于 NgModule 的更多知识

More on NgModules

你可能还对下列内容感兴趣:

You may also be interested in the following: