特性模块的分类
Types of Feature Modules
前提条件
Prerequisites
对下列概念有基本的理解:
A basic understanding of the following concepts:
常用模块。
下面是特性模块的五个常用分类,包括五组:
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. 领域特性模块大部分由 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. 对于缺少路由的小型应用,它们可能只会被根模块 They might be imported by the root |
路由 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. 惰性加载的路由特性模块不应该被任何模块导入。如果那样做就会导致它被急性加载,破坏了惰性加载的设计用途。 也就是说你应该永远不会看到它们在 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 路由特性模块很少会有服务提供商,原因参见惰性加载的特性模块中的解释。如果那样做,那么它所提供的服务的生命周期必须与该模块的生命周期完全相同。不要在路由特性模块或被路由特性模块所导入的模块中提供全应用级的单例服务。 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:
路由模块只应该被它的配套模块导入。 A routing module should only be imported by its companion module. |
服务 Service | 服务模块提供了一些工具服务,比如数据访问和消息。理论上,它们应该是完全由服务提供商组成的,不应该有可声明对象。Angular 的 Service modules provide utility services such as data access and messaging. Ideally, they consist entirely of providers and have no declarations. Angular's 根模块 The root |
窗口部件 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 | 提供商 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: