简体   繁体   English

Angular2 - 模块与组件有何不同?

[英]Angular2 - How module is different from component?

In the below code from ../src/app/app.module.ts , 在以下代码中来自../src/app/app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';

Component consists: 组件包括:

  • view( ../src/app/app.component.html ) view( ../src/app/app.component.html

  • logic( ../src/app/app.component.ts ) 逻辑( ../src/app/app.component.ts

  • style( ../src/app/app.component.css ) style( ../src/app/app.component.css

Angular application is a tree of components. 角度应用程序是一个组件树。 Good components have high cohesion , ie each component contains only elements with related functionality. 良好的组件具有高内聚力 ,即每个组件仅包含具有相关功能的元件。 They are also well encapsulated and loosely coupled . 它们也很好地封装和松散耦合


How modules are different from components? 模块与组件有何不同?

A component is just a class with the @Component() annotation. 组件只是一个带有@Component()注释的@Component() Note that .html and .css files might be referenced by the component, certainly not mandatory. 请注意,组件可能会引用.html和.css文件,当然不是必需的。 The component template might very well be 'inlined' directly in the component configuration, or there simply might not be any html template at all for a given component. 组件模板很可能直接在组件配置中“内联”,或者根本不存在任何给定组件的html模板。

A module is a structural element of an Angular application (and maybe other classes and interfaces). 模块是Angular应用程序(以及其他类和接口)的结构元素。 It is also "just a class" with the @NgModule() annotation. 它也是@NgModule()注释的“只是一个类”。

It acts as a logical 'container' for your components, directives, services, pipes, etc... to help you structure your overall source code better. 它充当您的组件,指令,服务,管道等的逻辑“容器”......以帮助您更好地构建整体源代码。

You can have a look at this existing question : What's the difference between an Angular component and module 您可以查看这个现有问题: Angular组件和模块之间的区别

A module is something that has components. 模块是具有组件的东西。 It wraps them up so you can import and manage them. 它将它们包装起来,以便您可以导入和管理它们。

Notice when you make a component you can put anything that's decorated as @Injectable in your constructor: 注意,当你创建一个组件时,你可以在构造函数中放置任何被装饰为@Injectable东西:

@Component({
  selector: 'app-heroes',
  templateUrl: './heroes.component.html',
  styleUrls: ['./heroes.component.css']
})
export class HeroesComponent implements OnInit {

  constructor(private myService: MyService) { }

  ngOnInit() {
  }

}

And magically you will have a myService to use. 神奇的是,你将使用myService This is dependency injection, which is built into Angular - but it's managed on a Module level. 这是依赖注入,它内置于Angular中 - 但它是在Module级别上进行管理的。 In your module you import what other modules you want to be able to use: 在您的模块中,您可以导入您希望能够使用的其他模块:

imports: [
  BrowserModule,
  FormsModule
],

define what your module includes: 定义您的模块包含的内容:

declarations: [
  AppComponent,
  HeroesComponent,
  MyService
],

export any components (so other modules can import them) 导出任何组件(以便其他模块可以导入它们)

exports: [
  HeroesComponent
],

They help organize an application into blocks of functionality. 它们有助于将应用程序组织成功能块。 Components are things that tell angular how to render something. 组件是告诉角度如何渲染某些东西的东西。 Modules compose Components, Pipes, Services etc into 'blocks' that can be compiled by angular or imported and used by others. 模块将组件,管道,服务等组合成“块”,可以通过角度编译或导入并由其他人使用。

Edit to address comment 编辑以解决评论

Taking your specific question about HttpClient . 提出有关HttpClient的具体问题。 The HttpClient is the service you are using to perform the actions. HttpClient是您正在使用执行的操作的服务 The HttpClientModule is the module you import into your module, so you can use the service it contains. HttpClientModule是您导入到您的模块模块 ,所以你可以使用它包含的服务

You import the module: 您导入模块:

@NgModule({
  imports: [
    BrowserModule,
    // Include it under 'imports' in your application module
    // after BrowserModule.
    HttpClientModule,
  ],
})

And use the service: 使用该服务:

@Component(...)
export class MyComponent implements OnInit {

  // Inject HttpClient into your component or service.
  constructor(private http: HttpClient) {}

  ...
}

The HttpClientModule contains within it all you need for the HttpClient to work, and packages it up so you can use it in your own projects. HttpClientModule包含HttpClient工作所需的所有内容,并将其打包,以便您可以在自己的项目中使用它。

This particular module only wraps up that one service, but the module could contain a bunch of related services, components, pipes or directives. 此特定模块仅包装该服务,但该模块可能包含一堆相关的服务,组件,管道或指令。 For example, the RouterModule allows you to use the RouterOutlet and RouterLink directives. 例如, RouterModule允许您使用RouterOutletRouterLink指令。

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. 角度模块也是组件,服务,过滤器或其他一些较小模块的集合,或者我们可以说您导入所有这些模块的位置,以便稍后在应用程序中使用以备将来使用。 in a single app there can be one or more than one module may exist. 在单个应用程序中,可能存在一个或多个模块。

Whereas, A component controls a patch of screen called a view. 然而,A 组件控制称为视图的屏幕补丁。 You define a component's application logic—what it does to support the view—inside a class. 您可以定义组件的应用程序逻辑 - 它在类中支持视图的作用。 The class interacts with the view through an API of properties and methods. 该类通过属性和方法的API与视图交互。

Refer this guide for more details: 有关详细信息,请参阅本指南:

https://angular.io/guide/architecture https://angular.io/guide/architecture

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

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