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 服务 | Service modules provide utility services such as data access and messaging. Ideally, they consist entirely of providers and have no declarations. Angular's 服务模块提供了一些工具服务,比如数据访问和消息。理论上,它们应该是完全由服务提供商组成的,不应该有可声明对象。Angular 的 The root 根模块 |
Widget 窗口部件 | A widget module makes components, directives, and pipes available to external modules. Many third-party UI component libraries are widget modules. 窗口部件模块为外部模块提供组件、指令和管道。很多第三方 UI 组件库都是窗口部件模块。 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 顶级组件 | Feature, AppModule 特性模块,AppModule |
Routed 路由 | Yes 有 | Rare 罕见 | No 无 | None 无 |
Routing 路由 | No 无 | Yes (Guards) 是(守卫) | RouterModule | Feature (for routing) 特性(供路由使用) |
Service 服务 | No 无 | Yes 有 | No 无 | AppModule |
Widget 窗口部件 | Yes 有 | Rare 罕见 | Yes 有 | Feature 特性 |
More on NgModules
关于 NgModule 的更多知识
You may also be interested in the following:
你可能还对下列内容感兴趣: