简体   繁体   English

Angular2:模块和组件的区别

[英]Angular2: Module and Component difference

I don't quite to figure out, why Angular2 have two concepts? 我不太清楚,为什么Angular2有两个概念?

  1. Module

  2. Component 零件

What is the difference between them and which should be the role they would play? 它们之间有什么区别,它们应扮演的角色应该是什么?

  • When would i need to create a SubModule ? 我什么时候需要创建一个SubModule
  • When would i need to create a SubComponent ? 我什么时候需要创建一个SubComponent

Module and Component have hardly anything in common, except that they are classes with decorators and providers can registered with them. 模块和组件几乎没有任何共同之处,除了它们是带有装饰器的类,提供者可以在它们中注册。

A component is a visible part of your application with optional behavior (event handlers) 组件是应用程序的可见部分,具有可选行为(事件处理程序)

Modules were introduced to split your application for lazy loading. 引入了模块来拆分延迟加载的应用程序。

  • SubModule is usually a set of services, components, directives, and pipes that build together a reusable feature. SubModule通常是一组服务,组件,指令和管道,它们共同构建了一个可重用的功能。 By adding a module to imports: [...] of another module, all its content is made available to the importing module. 通过向另一个模块的imports: [...]添加模块,其所有内容都可供导入模块使用。

  • SubComponent is not really a term in Angular2. SubComponent实际上不是Angular2中的术语。 There are just components. 只有组件。 One special component is the root component. 一个特殊组件是根组件。 The difference is that the root component is created by bootstrapping an Angular2 application, while other components are created because their selector matches HTML in the view of another component. 不同之处在于根组件是通过引导Angular2应用程序创建的,而其他组件是由于其选择器在另一个组件的视图中与HTML匹配而创建的。

Therefor, except of the root component all components are subcomponents. 因此,除根组件外,所有组件都是子组件。

Basically Module and Component both are different terms. 基本上模块和组件都是不同的术语。

How ? 怎么样 ?

Module in Angular is set of Components, Services, Filters, or some another smaller modules too, or we can say where you import all these in order to use later in the app for future use. Angular中的模块也是组件,服务,过滤器或其他一些较小的模块,或者我们可以说您导入所有这些模块的位置,以便稍后在应用程序中使用以供将来使用。 in a single app there can be one or more than one module may exist. 在单个应用程序中,可能存在一个或多个模块。

Whereas Component is a single layer or view to perform some actions etc. components can be lazy loaded as per requirements. 组件是单个层或视图以执行某些操作等。组件可以根据需要延迟加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM