angular-cn/aio/content/guide/architecture-components.md

331 lines
22 KiB
Markdown
Raw Normal View History

2018-03-24 01:12:42 -04:00
# Introduction to components
# 组件简介
2018-03-24 01:12:42 -04:00
<img src="generated/images/guide/architecture/hero-component.png" alt="Component" class="left">
A _component_ controls a patch of screen called a *view*. For example, individual components define and control each of the following views from the [Tutorial](tutorial/index):
*组件*控制屏幕上被称为*视图*的一小片区域。比如,[教程](tutorial/index)中的下列视图都是由一个个组件所定义和控制的:
2018-03-24 01:12:42 -04:00
* The app root with the navigation links.
带有导航链接的应用根组件。
* The list of heroes.
英雄列表。
* The hero editor.
英雄编辑器。
You define a component's application logic&mdash;what it does to support the view&mdash;inside a class.
The class interacts with the view through an API of properties and methods.
2018-03-24 01:12:42 -04:00
你在类中定义组件的应用逻辑,为视图提供支持。
组件通过一些由属性和方法组成的 API 与视图交互。
For example, the `HeroListComponent` has a `heroes` property that holds an array of heroes. It also has a `selectHero()` method that sets a `selectedHero` property when the user clicks to choose a hero from that list. The component acquires the heroes from a service, which is a TypeScript [parameter property](http://www.typescriptlang.org/docs/handbook/classes.html#parameter-properties) on the constructor. The service is provided to the component through the dependency injection system.
2018-03-24 01:12:42 -04:00
比如,`HeroListComponent` 有一个 `heroes` 属性,它会返回一个从服务中取到的英雄数组。
`HeroListComponent` 还有一个 `selectHero()` 方法,当用户从列表中选择一个英雄时,它会设置 `selectedHero` 属性的值。
2018-07-27 08:22:07 -04:00
该组件会从服务获取英雄列表,它是一个 TypeScript 的构造器[参数型属性](http://www.typescriptlang.org/docs/handbook/classes.html#parameter-properties)。本服务通过依赖注入系统提供给该组件。
2018-03-24 01:12:42 -04:00
<code-example path="architecture/src/app/hero-list.component.ts" linenums="false" title="src/app/hero-list.component.ts (class)" region="class"></code-example>
Angular creates, updates, and destroys components as the user moves through the application. Your app can take action at each moment in this lifecycle through optional [lifecycle hooks](guide/lifecycle-hooks), like `ngOnInit()`.
当用户在应用中穿行时Angular 就会创建、更新、销毁一些组件。
你的应用可以通过一些可选的[生命周期钩子](guide/lifecycle-hooks)(比如`ngOnInit()`)来在每个特定的时机采取行动。
2018-03-24 01:12:42 -04:00
<hr/>
## Component metadata
## 组件的元数据
2018-03-24 01:12:42 -04:00
<img src="generated/images/guide/architecture/metadata.png" alt="Metadata" class="left">
2018-05-07 01:06:47 -04:00
The `@Component` decorator identifies the class immediately below it as a component class, and specifies its metadata. In the example code below, you can see that `HeroListComponent` is just a class, with no special Angular notation or syntax at all. It's not a component until you mark it as one with the `@Component` decorator.
2018-03-24 01:12:42 -04:00
`@Component` 装饰器会指出紧随其后的那个类是个组件类,并为其指定元数据。
在下面的范例代码中,你可以看到 `HeroListComponent` 只是一个普通类,完全没有 Angular 特有的标记或语法。
直到给它加上了 `@Component` 装饰器,它才变成了组件。
2018-03-24 01:12:42 -04:00
The metadata for a component tells Angular where to get the major building blocks it needs to create and present the component and its view. In particular, it associates a _template_ with the component, either directly with inline code, or by reference. Together, the component and its template describe a _view_.
组件的元数据告诉 Angular 到哪里获取它需要的主要构造块,以创建和展示这个组件及其视图。
具体来说,它把一个*模板*(无论是直接内联在代码中还是引用的外部文件)和该组件关联起来。
该组件及其模板,共同描述了一个*视图*。
2018-03-24 01:12:42 -04:00
In addition to containing or pointing to the template, the `@Component` metadata configures, for example, how the component can be referenced in HTML and what services it requires.
除了包含或指向模板之外,`@Component` 的元数据还会配置要如何在 HTML 中引用该组件,以及该组件需要哪些服务等等。
2018-03-24 01:12:42 -04:00
Here's an example of basic metadata for `HeroListComponent`:
下面的例子中就是 `HeroListComponent` 的基础元数据:
2018-03-24 01:12:42 -04:00
<code-example path="architecture/src/app/hero-list.component.ts" linenums="false" title="src/app/hero-list.component.ts (metadata)" region="metadata"></code-example>
This example shows some of the most useful `@Component` configuration options:
这个例子展示了一些最常用的 `@Component` 配置选项:
2018-03-24 01:12:42 -04:00
* `selector`: A CSS selector that tells Angular to create and insert an instance of this component wherever it finds the corresponding tag in template HTML. For example, if an app's HTML contains `<app-hero-list></app-hero-list>`, then
Angular inserts an instance of the `HeroListComponent` view between those tags.
2018-05-07 01:06:47 -04:00
`selector`:是一个 CSS 选择器,它会告诉 Angular一旦在模板 HTML 中找到了这个选择器对应的标签,就创建并插入该组件的一个实例。
比如,如果应用的 HTML 中包含 `<app-hero-list></app-hero-list>`Angular 就会在这些标签中插入一个 `HeroListComponent` 实例的视图。
2018-03-24 01:12:42 -04:00
* `templateUrl`: The module-relative address of this component's HTML template. Alternatively, you can provide the HTML template inline, as the value of the `template` property. This template defines the component's _host view_.
2018-05-07 01:06:47 -04:00
`templateUrl`:该组件的 HTML 模板文件相对于这个组件文件的地址。
另外,你还可以用 `template` 属性的值来提供内联的 HTML 模板。
这个模板定义了该组件的*宿主视图*。
* `providers`: An array of **dependency injection providers** for services that the component requires. In the example, this tells Angular how to provide the `HeroService` instance that the component's constructor uses to get the list of heroes to display.
2018-03-24 01:12:42 -04:00
2018-07-27 08:22:07 -04:00
`providers` 是当前组件所需的**依赖注入提供商**的一个数组。在这个例子中,它告诉 Angular 该如何提供一个 `HeroService` 实例,以获取要显示的英雄列表。
2018-03-24 01:12:42 -04:00
<hr/>
## Templates and views
## 模板与视图
2018-03-24 01:12:42 -04:00
<img src="generated/images/guide/architecture/template.png" alt="Template" class="left">
You define a component's view with its companion template. A template is a form of HTML that tells Angular how to render the component.
你要通过组件的配套模板来定义其视图。模板就是一种 HTML它会告诉 Angular 如何渲染该组件。
2018-03-24 01:12:42 -04:00
Views are typically arranged hierarchically, allowing you to modify or show and hide entire UI sections or pages as a unit. The template immediately associated with a component defines that component's _host view_. The component can also define a _view hierarchy_, which contains _embedded views_, hosted by other components.
视图通常会分层次进行组织,让你能以 UI 分区或页面为单位进行修改、显示或隐藏。
与组件直接关联的模板会定义该组件的*宿主视图*。该组件还可以定义一个*带层次结构的视图*,它包含一些*内嵌的视图*作为其它组件的宿主。
2018-03-24 01:12:42 -04:00
<figure>
<img src="generated/images/guide/architecture/component-tree.png" alt="Component tree" class="left">
</figure>
A view hierarchy can include views from components in the same NgModule, but it also can (and often does) include views from components that are defined in different NgModules.
带层次结构的视图可以包含同一模块NgModule中组件的视图也可以而且经常会包含其它模块中定义的组件的视图。
2018-03-24 01:12:42 -04:00
## Template syntax
## 模板语法
2018-03-24 01:12:42 -04:00
A template looks like regular HTML, except that it also contains Angular [template syntax](guide/template-syntax), which alters the HTML based on your app's logic and the state of app and DOM data. Your template can use _data binding_ to coordinate the app and DOM data, _pipes_ to transform data before it is displayed, and _directives_ to apply app logic to what gets displayed.
模板很像标准的 HTML但是它还包含 Angular 的[模板语法](guide/template-syntax),这些模板语法可以根据你的应用逻辑、应用状态和 DOM 数据来修改这些 HTML。
你的模板可以使用*数据绑定*来协调应用和 DOM 中的数据,使用*管道*在显示出来之前对其进行转换,使用*指令*来把程序逻辑应用到要显示的内容上。
2018-03-24 01:12:42 -04:00
For example, here is a template for the Tutorial's `HeroListComponent`:
比如,下面是本教程中 `HeroListComponent` 的模板:
2018-03-24 01:12:42 -04:00
<code-example path="architecture/src/app/hero-list.component.html" title="src/app/hero-list.component.html"></code-example>
This template uses typical HTML elements like `<h2>` and `<p>`, and also includes Angular template-syntax elements, `*ngFor`, `{{hero.name}}`, `(click)`, `[hero]`, and `<app-hero-detail>`. The template-syntax elements tell Angular how to render the HTML to the screen, using program logic and data.
这个模板使用了典型的 HTML 元素,比如 `<h2>``<p>`,还包括一些 Angular 的模板语法元素,如 `*ngFor``{{hero.name}}``click`、`[hero]` 和 `<app-hero-detail>`。这些模板语法元素告诉 Angular 该如何根据程序逻辑和数据在屏幕上渲染 HTML。
2018-03-24 01:12:42 -04:00
* The `*ngFor` directive tells Angular to iterate over a list.
2018-05-07 01:06:47 -04:00
`*ngFor` 指令告诉 Angular 在一个列表上进行迭代。
2018-03-24 01:12:42 -04:00
* The `{{hero.name}}`, `(click)`, and `[hero]` bind program data to and from the DOM, responding to user input. See more about [data binding](#data-binding) below.
2018-05-07 01:06:47 -04:00
`{{hero.name}}`、`(click)` 和 `[hero]` 把程序数据绑定到及绑定回 DOM以响应用户的输入。更多内容参见稍后的[数据绑定](#data-binding)部分。
2018-03-24 01:12:42 -04:00
* The `<app-hero-detail>` tag in the example is an element that represents a new component, `HeroDetailComponent`. The `HeroDetailComponent` (code not shown) is a child component of the `HeroListComponent` that defines the Hero-detail view. Notice how custom components like this mix seamlessly with native HTML in the same layouts.
2018-05-07 01:06:47 -04:00
模板中的 `<app-hero-detail>` 标签是一个代表新组件 `HeroDetailComponent` 的元素。
`HeroDetailComponent`(代码略)是 `HeroListComponent` 的一个子组件,它定义了英雄详情视图。
注意观察像这样的自定义组件是如何与原生 HTML 元素无缝的混合在一起的。
2018-03-24 01:12:42 -04:00
### Data binding
### 数据绑定
2018-03-24 01:12:42 -04:00
Without a framework, you would be responsible for pushing data values into the HTML controls and turning user responses into actions and value updates. Writing such push/pull logic by hand is tedious, error-prone, and a nightmare to read, as any experienced jQuery programmer can attest.
如果没有框架,你就要自己负责把数据值推送到 HTML 控件中,并把来自用户的响应转换成动作和对值的更新。
手动写这种数据推拉逻辑会很枯燥、容易出错,难以阅读 —— 用过 jQuery 的程序员一定深有体会。
2018-05-07 01:06:47 -04:00
Angular supports *two-way data binding*, a mechanism for coordinating parts of a template with parts of a component. Add binding markup to the template HTML to tell Angular how to connect both sides.
2018-03-24 01:12:42 -04:00
Angular 支持*双向数据绑定*,这是一种对模板中的各个部件与组件中的各个部件进行协调的机制。
往模板 HTML 中添加绑定标记可以告诉 Angular 该如何连接它们。
2018-03-24 01:12:42 -04:00
The following diagram shows the four forms of data binding markup. Each form has a direction&mdash;to the DOM, from the DOM, or in both directions.
下图显示了数据绑定标记的四种形式。每种形式都有一个方向 —— 从组件到 DOM、从 DOM 到组件或双向。
2018-03-24 01:12:42 -04:00
<figure>
<img src="generated/images/guide/architecture/databinding.png" alt="Data Binding" class="left">
</figure>
This example from the `HeroListComponent` template uses three of these forms:
这个来自 `HeroListComponent` 模板中的例子使用了其中的三种形式:
2018-03-24 01:12:42 -04:00
<code-example path="architecture/src/app/hero-list.component.1.html" linenums="false" title="src/app/hero-list.component.html (binding)" region="binding"></code-example>
* The `{{hero.name}}` [*interpolation*](guide/displaying-data#interpolation)
displays the component's `hero.name` property value within the `<li>` element.
`{{hero.name}}`[*插值表达式*](guide/displaying-data#interpolation)在 `<li>` 标签中显示组件的 `hero.name` 属性的值。
* The `[hero]` [*property binding*](guide/template-syntax#property-binding) passes the value of `selectedHero` from
the parent `HeroListComponent` to the `hero` property of the child `HeroDetailComponent`.
`[hero]`[*属性绑定*](guide/template-syntax#property-binding)把父组件 `HeroListComponent``selectedHero` 的值传到子组件 `HeroDetailComponent``hero` 属性中。
* The `(click)` [*event binding*](guide/user-input#binding-to-user-input-events) calls the component's `selectHero` method when the user clicks a hero's name.
2018-05-07 01:06:47 -04:00
当用户点击某个英雄的名字时,`(click)` [*事件绑定*](guide/user-input#binding-to-user-input-events)会调用组件的 `selectHero` 方法。
2018-03-24 01:12:42 -04:00
**Two-way data binding** is an important fourth form that combines property and event binding in a single notation. Here's an example from the `HeroDetailComponent` template that uses two-way data binding with the `ngModel` directive:
第四种重要的绑定形式是**双向数据绑定**,它把属性绑定和事件绑定组合成一种单独的写法。下面这个来自 `HeroDetailComponent` 模板中的例子通过 `ngModel` 指令使用了双向数据绑定:
2018-03-24 01:12:42 -04:00
<code-example path="architecture/src/app/hero-detail.component.html" linenums="false" title="src/app/hero-detail.component.html (ngModel)" region="ngModel"></code-example>
In two-way binding, a data property value flows to the input box from the component as with property binding.
The user's changes also flow back to the component, resetting the property to the latest value,
as with event binding.
在双向绑定中,数据属性值通过属性绑定从组件流到输入框。用户的修改通过事件绑定流回组件,把属性值设置为最新的值。
Angular processes *all* data bindings once per JavaScript event cycle,
from the root of the application component tree through all child components.
Angular 在每个 JavaScript 事件循环中处理*所有的*数据绑定,它会从组件树的根部开始,递归处理全部子组件。
<figure>
<img src="generated/images/guide/architecture/component-databinding.png" alt="Data Binding" class="left">
</figure>
Data binding plays an important role in communication between a template and its component, and is also important for communication between parent and child components.
数据绑定在模板及其组件之间的通讯中扮演了非常重要的角色,它对于父组件和子组件之间的通讯也同样重要。
2018-03-24 01:12:42 -04:00
<figure>
<img src="generated/images/guide/architecture/parent-child-binding.png" alt="Parent/Child binding" class="left">
</figure>
### Pipes
### 管道
Angular pipes let you declare display-value transformations in your template HTML. A class with the `@Pipe` decorator defines a function that transforms input values to output values for display in a view.
Angular 的管道可以让你在模板中声明显示值的转换逻辑。
带有 `@Pipe` 装饰器的类中会定义一个转换函数,用来把输入值转换成供视图显示用的输出值。
2018-03-24 01:12:42 -04:00
Angular defines various pipes, such as the [date](https://angular.io/api/common/DatePipe) pipe and [currency](https://angular.io/api/common/CurrencyPipe) pipe; for a complete list, see the [Pipes API list](https://angular.io/api?type=pipe). You can also define new pipes.
Angular 自带了很多管道,比如 [date](https://angular.cn/api/common/DatePipe) 管道和 [currency](https://angular.cno/api/common/CurrencyPipe) 管道,完整的列表参见 [Pipes API 列表](https://angular.cn/api?type=pipe)。你也可以自己定义一些新管道。
2018-03-24 01:12:42 -04:00
To specify a value transformation in an HTML template, use the [pipe operator (|)](https://angular.io/guide/template-syntax#pipe):
要在 HTML 模板中指定值的转换方式,请使用 [管道操作符 (|)](https://angular.cn/guide/template-syntax#pipe)。
2018-03-24 01:12:42 -04:00
`{{interpolated_value | pipe_name}}`
You can chain pipes, sending the output of one pipe function to be transformed by another pipe function. A pipe can also take arguments that control how it performs its transformation. For example, you can pass the desired format to the `date` pipe:
你可以把管道串联起来,把一个管道函数的输出送给另一个管道函数进行转换。
管道还能接收一些参数,来控制它该如何进行转换。比如,你可以把要使用的日期格式传给 `date` 管道:
2018-03-24 01:12:42 -04:00
```
<!-- Default format: output 'Jun 15, 2015'-->
<p>Today is {{today | date}}</p>
<!-- fullDate format: output 'Monday, June 15, 2015'-->
<p>The date is {{today | date:'fullDate'}}</p>
<!-- shortTime format: output '9:43 AM'-->
<p>The time is {{today | date:'shortTime'}}</p>
```
<hr/>
### Directives
### 指令
2018-03-24 01:12:42 -04:00
<img src="generated/images/guide/architecture/directive.png" alt="Directives" class="left">
Angular templates are *dynamic*. When Angular renders them, it transforms the DOM according to the instructions given by *directives*. A directive is a class with a `@Directive` decorator.
Angular 的模板是*动态的*。当 Angular 渲染它们的时候,会根据*指令*给出的指示对 DOM 进行转换。
指令就是一个带有 `@Directive` 装饰器的类。
2018-03-24 01:12:42 -04:00
A component is technically a directive - but components are so distinctive and central to Angular applications that Angular defines the `@Component` decorator, which extends the `@Directive` decorator with template-oriented features.
组件从技术角度上说就是一个指令,但是由于组件对 Angular 应用来说非常独特、非常重要,因此 Angular 专门定义了 `@Component` 装饰器,它使用一些面向模板的特性扩展了 `@Directive` 装饰器。
2018-03-24 01:12:42 -04:00
There are two kinds of directives besides components: _structural_ and _attribute_ directives. Just as for components, the metadata for a directive associates the class with a `selector` that you use to insert it into HTML. In templates, directives typically appear within an element tag as attributes, either by name or as the target of an assignment or a binding.
除组件外,还有两种指令:*结构型指令*和*属性型指令*。和组件一样,指令的元数据把指令类和一个 `selector` 关联起来,`selector` 用来把该指令插入到 HTML 中。
在模板中,指令通常作为属性出现在元素标签上,可能仅仅作为名字出现,也可能作为赋值目标或绑定目标出现。
2018-03-24 01:12:42 -04:00
#### Structural directives
#### 结构型指令
2018-03-24 01:12:42 -04:00
Structural directives alter layout by adding, removing, and replacing elements in DOM. The example template uses two built-in structural directives to add application logic to how the view is rendered:
结构型指令通过添加、移除或替换 DOM 元素来修改布局。
这个范例模板使用了两个内置的结构型指令来为要渲染的视图添加程序逻辑:
2018-03-24 01:12:42 -04:00
<code-example path="architecture/src/app/hero-list.component.1.html" linenums="false" title="src/app/hero-list.component.html (structural)" region="structural"></code-example>
* [`*ngFor`](guide/displaying-data#ngFor) is an iterative; it tells Angular to stamp out one `<li>` per hero in the `heroes` list.
2018-05-07 01:06:47 -04:00
[`*ngFor`](guide/displaying-data#ngFor) 是一个迭代器,它要求 Angular 为 `heroes` 列表中的每个 `<li>` 渲染出一个 `<li>`
2018-03-24 01:12:42 -04:00
* [`*ngIf`](guide/displaying-data#ngIf) is a conditional; it includes the `HeroDetail` component only if a selected hero exists.
2018-05-07 01:06:47 -04:00
[`*ngIf`](guide/displaying-data#ngIf) 是个条件语句,只有当选中的英雄存在时,它才会包含 `HeroDetail` 组件。
2018-03-24 01:12:42 -04:00
#### Attribute directives
#### 属性型指令
2018-03-24 01:12:42 -04:00
Attribute directives alter the appearance or behavior of an existing element.
In templates they look like regular HTML attributes, hence the name.
属性型指令会修改现有元素的外观或行为。
在模板中,它们看起来就像普通的 HTML 属性一样,因此得名“属性型指令”。
2018-03-24 01:12:42 -04:00
The `ngModel` directive, which implements two-way data binding, is an example of an attribute directive. `ngModel` modifies the behavior of an existing element (typically an `<input>`) by setting its display value property and responding to change events.
`ngModel` 指令就是属性型指令的一个例子,它实现了双向数据绑定。
`ngModel` 修改现有元素(一般是 `<input>`)的行为:设置其显示属性值,并响应 change 事件。
<code-example path="architecture/src/app/hero-detail.component.html" linenums="false" title="src/app/hero-detail.component.html (ngModel)" region="ngModel"></code-example>
Angular has more pre-defined directives that either alter the layout structure
(for example, [ngSwitch](guide/template-syntax#ngSwitch))
or modify aspects of DOM elements and components
(for example, [ngStyle](guide/template-syntax#ngStyle) and [ngClass](guide/template-syntax#ngClass)).
Angular 还有很多预定义指令,它们或者修改布局结构(比如 [ngSwitch](guide/template-syntax#ngSwitch)),或者修改 DOM 元素和组件的某些方面(比如 [ngStyle](guide/template-syntax#ngStyle) 和 [ngClass](guide/template-syntax#ngClass))。
2018-03-24 01:12:42 -04:00
You can also write your own directives. Components such as `HeroListComponent` are one kind of custom directive. You can also create custom structural and attribute directives.
你还可以写自己的指令。像 `HeroListComponent` 这样的组件就是一种自定义指令,你还可以创建自定义的结构型指令和属性型指令。
2018-03-24 01:12:42 -04:00
<!-- PENDING: link to where to learn more about other kinds! -->