fix: 列表错误

This commit is contained in:
Zhicheng WANG 2019-06-08 17:59:28 +08:00
parent cc301263b8
commit b6b3322e78
5 changed files with 0 additions and 327 deletions

View File

@ -2,18 +2,15 @@
# 管理数据
At the end of [Routing](start/routing "Getting Started: Routing"), the online store application has a product catalog with two views: a product list and product details.
Users can click on a product name from the list to see details in a new view, with a distinct URL (route).
在[路由](start/routing "入门:路由")的末尾,本应用实现了一个包含两个视图的商品名录:商品列表和商品详情。用户点击清单中的某个商品名称,就会在新视图中看到具有显著 URL路由的详情页。
In this section, you'll create the shopping cart. You'll:
在本节中,你将创建购物车。你将:
* Update the product details page to include a "Buy" button, which adds the current product to a list of products managed by a cart service.
修改商品详情页,让它包含一个 “Buy” 按钮,它会把当前商品添加到由 "购物车服务" 管理的商品列表中。
@ -26,47 +23,39 @@ In this section, you'll create the shopping cart. You'll:
添加一个配送组件,它会使用 Angular 的 `HttpClient``.json` 文件中检索配送数据来取得购物车中这些商品的运费。
{@a services}
## Services
## 服务
Services are an integral part of Angular applications. In Angular, a service is an instance of a class that can be made available to any part of your application using Angular's [dependency injection system](guide/glossary#dependency-injection-di "dependency injection definition").
服务是 Angular 应用的重要组成部分。在 Angular 中,服务是一个类的实例,它可以借助 Angular 的[依赖注入系统](guide/glossary#dependency-injection-di "依赖注入定义")来让应用中的任何一个部件都能使用它。
Services are the place where you share data between parts of your application. For the online store, the cart service is where you store your cart data and methods.
服务可以让你在应用的各个部件之间共享数据。对于在线商店,购物车服务就是存放购物车的数据和方法的地方。
{@a create-cart-service}
## Create the shopping cart service
## 创建购物车服务
Up to this point, users can view product information, and simulate sharing and being notified about product changes. They cannot, however, buy products.
到目前为止,用户可以查看商品信息、模拟共享,并接收商品变化的通知。但是,无法购买商品。
In this section, you'll add a "Buy" button the product details page.
You'll also set up a cart service to store information about products in the cart.
在本节中你将在商品详情页中添加“Buy”按钮。你还可以设置一个购物车服务来存储购物车中商品的相关信息。
<div class="alert is-helpful">
Later, in the [Forms](start/forms "Getting Started: Forms") part of this tutorial, this cart service also will be accessed from the page where the user checks out.
稍后,在本教程的[表单](start/forms "入门:表单")部分,也会从用户的结账页面中访问这个 购物车服务。
</div>
{@a generate-cart-service}
@ -74,47 +63,32 @@ Later, in the [Forms](start/forms "Getting Started: Forms") part of this tutoria
### 定义购物车服务
1. Generate a cart service.
生成购物车服务。
1. Right click on the `app` folder, choose `Angular Generator`, and choose `Service`. Name the new service `cart`.
右键单击 `app` 文件夹,选择 `Angular Generator`,然后选择 `Service`。把新的服务命名为 `cart`
<code-example header="src/app/cart.service.ts" path="getting-started/src/app/cart.service.1.ts"></code-example>
1. If the generated `@Injectable()` decorator does not include the `{ providedIn: 'root' }` statement, then insert it as shown above.
如果生成的 `@Injectable()` 装饰器中没有包含 `{ providedIn: 'root' }` 语句,那就插入它,如上图所示。
1. In the `CartService` class, define an `items` property to store the list (array) of the current products in the cart.
`CartService` 类中,定义一个 `items` 属性来把当前商品的列表(数组)存储在购物车中。
<code-example path="getting-started/src/app/cart.service.ts" region="props"></code-example>
1. Define methods to add items to the cart, return cart items, and clear the cart items:
定义把商品添加到购物车、返回购物车商品以及清除购物车商品的方法:
<code-example path="getting-started/src/app/cart.service.ts" region="methods" linenums="false"></code-example>
<!--
To check: StackBlitz includes the constructor. If it's important (and not obvious) that the methods be below the constructor, then we should show it or say something.
-->
@ -132,29 +106,24 @@ Later, in the [Forms](start/forms "Getting Started: Forms") part of this tutoria
### 使用购物车服务
In this section, you'll update the product details component to use the cart service.
You'll add a "Buy" button to the product details view.
When the "Buy" button is clicked, you'll use the cart service to add the current product to the cart.
在本节中你将修改商品详情组件以使用这个购物车服务。你可以在商品详情视图中添加一个“Buy”按钮。单击“Buy”按钮后你将借助购物车服务来把当前商品添加到购物车中。
1. Open `product-details.component.ts`.
打开 `product-details.component.ts`
1. Set up the component to be able to use the cart service.
设置该组件,使其能使用这个购物车服务。
1. Import the cart service.
导入购物车服务。
<code-example header="src/app/product-details/product-details.component.ts" path="getting-started/src/app/product-details/product-details.component.ts" region="cart-service">
</code-example>
@ -162,7 +131,6 @@ When the "Buy" button is clicked, you'll use the cart service to add the current
注入购物车服务。
<code-example path="getting-started/src/app/product-details/product-details.component.ts" region="inject-cart-service">
</code-example>
@ -174,12 +142,10 @@ When the "Buy" button is clicked, you'll use the cart service to add the current
定义 `addToCart()` 方法,该方法会当前商品添加到购物车中。
The `addToCart()` method:
`addToCart()` 方法:
* Receives the current `product`
收到当前的 `product`
@ -192,27 +158,20 @@ When the "Buy" button is clicked, you'll use the cart service to add the current
显示一条 "商品已添加到购物车" 的消息
<code-example path="getting-started/src/app/product-details/product-details.component.ts" region="add-to-cart"></code-example>
1. Update the product details template to have a "Buy" button that adds the current product to the cart.
修改商品详情模板让它具有一个“Buy”按钮用于把当前商品添加到购物车中。
1. Open `product-details.component.html`.
打开 `product-details.component.html`
1. Add a button with the label "Buy", and bind the `click()` event to the `addToCart()` method:
添加一个标签为“Buy”的按钮并把其 `click()` 事件绑定到 `addToCart()` 方法:
<code-example header="src/app/product-details/product-details.component.html" path="getting-started/src/app/product-details/product-details.component.html">
</code-example>
@ -220,7 +179,6 @@ When the "Buy" button is clicked, you'll use the cart service to add the current
要查看新的“Buy”按钮请刷新应用并单击商品名称以显示其详细信息。
<figure>
<img src='generated/images/guide/start/product-details-buy.png' alt="Display details for selected product with a Buy button">
</figure>
@ -229,7 +187,6 @@ When the "Buy" button is clicked, you'll use the cart service to add the current
点击“Buy”按钮。该商品已添加到了购物车中存储的商品列表中并显示一条消息。
<figure>
<img src='generated/images/guide/start/buy-alert.png' alt="Display details for selected product with a Buy button">
</figure>
@ -238,17 +195,14 @@ When the "Buy" button is clicked, you'll use the cart service to add the current
## 创建购物车页面
At this point, users can put items in the cart by clicking "Buy", but they can't yet see their cart.
此时用户可以通过点击“Buy”来把商品放入购物车但他们还看不到购物车。
We'll create the cart page in two steps:
我们将分两步来创建购物车页面:
1. Create a cart component and set up routing to the new component. At this point, the cart page will only have default text.
创建一个购物车组件并设置到这个新组件的路由。此时,购物车页面只会显示默认文本。
@ -257,42 +211,32 @@ We'll create the cart page in two steps:
显示购物车商品
### Set up the component
### 设置该组件
To create the cart page, you begin by following the same steps you did to create the product details component and to set up routing for the new component.
要创建购物车页面,首先要执行与创建商品详情组件相同的步骤,并为这个新组件设置路由。
1. Generate a cart component, named `cart`.
生成一个名叫 `cart` 的购物车组件。
Reminder: In the file list, right-click the `app` folder, choose `Angular Generator` and `Component`.
提示:在文件列表框中,右键单击 `app` 文件夹,选择 `Angular Generator``Component`
<code-example header="src/app/cart/cart.component.ts" path="getting-started/src/app/cart/cart.component.1.ts"></code-example>
1. Add routing (a URL pattern) for the cart component.
为购物车组件添加路由URL 模式)。
Reminder: Open `app.module.ts` and add a route for the component `CartComponent`, with a `path` of `cart`:
提示:打开 `app.module.ts`,为组件 `CartComponent` 添加一个路由,其路由为 `cart`
<code-example header="src/app/app.module.ts" path="getting-started/src/app/app.module.ts" region="cart-route">
</code-example>
@ -304,12 +248,10 @@ We'll create the cart page in two steps:
要查看新的购物车组件请点击“Checkout”按钮。你会看到默认文本“cart works!”,该 URL 的格式为 `https://getting-started.stackblitz.io/cart`,其中的 getting-started.stackblitz.io 部分可能与你的 StackBlitz 项目不同。
(Note: The "Checkout" button that we provided in the top-bar component was already configured with a `routerLink` for `/cart`.)
注意我们在顶栏组件中提供的“Checkout”按钮已经配置了指向 `/cart``routerLink`。)
<figure>
<img src='generated/images/guide/start/cart-works.png' alt="Display cart page before customizing">
</figure>
@ -318,12 +260,10 @@ We'll create the cart page in two steps:
### 显示购物车商品
Services can be used to share data across components:
服务可用于跨组件共享数据:
* The product details component already uses the cart service (`CartService`) to add products to the cart.
商品详情组件已经使用了购物车服务( `CartService` )来把商品添加到购物车中。
@ -332,22 +272,18 @@ Services can be used to share data across components:
在本节中,你将修改购物车组件以使用购物车服务来显示购物车中的商品。
1. Open `cart.component.ts`.
打开 `cart.component.ts`
1. Set up the component to be able to use the cart service. (This is the same way you set up the product details component to use the cart service, above.)
设置该组件是为了让它能够使用这个购物车服务。(这与你在前面设置商品详情组件以使用购物车服务的方式是一样的。)
1. Import the `CartService` from the `cart.service.ts` file.
`cart.service.ts` 文件中导入 `CartService`
<code-example header="src/app/cart/cart.component.ts" path="getting-started/src/app/cart/cart.component.2.ts" region="imports">
</code-example>
@ -355,7 +291,6 @@ Services can be used to share data across components:
注入 `CartService` 来管理购物车信息。
<code-example path="getting-started/src/app/cart/cart.component.2.ts" region="inject-cart">
</code-example>
@ -363,7 +298,6 @@ Services can be used to share data across components:
定义 `items` 属性,以便把商品存放在购物车中。
<code-example path="getting-started/src/app/cart/cart.component.2.ts" region="items">
</code-example>
@ -371,12 +305,10 @@ Services can be used to share data across components:
使用购物车服务的 `getItems()` 方法设置这些商品。(你[在生成 `cart.service.ts` 时](#generate-cart-service)定义过这个方法。)
The resulting `CartComponent` class should look like this:
所生成的 `CartComponent` 类是这样的:
<code-example path="getting-started/src/app/cart/cart.component.3.ts" region="props-services">
</code-example>
@ -384,12 +316,10 @@ Services can be used to share data across components:
修改模板加上标题“Cart”用带有 `*ngFor``<div>` 来显示每个购物车商品的名字和价格。
The resulting `CartComponent` template should look like this:
生成的 `CartComponent` 模板如下:
<code-example header="src/app/cart/cart.component.html" path="getting-started/src/app/cart/cart.component.2.html" region="prices">
</code-example>
@ -397,7 +327,6 @@ Services can be used to share data across components:
测试你的购物车组件。
1. Click on "My Store" to go to the product list page.
点击“My Store”进入商品列表页面。
@ -418,7 +347,6 @@ Services can be used to share data across components:
要添加其它商品请点击“My Store”返回商品列表。重复上述步骤。
<figure>
<img src='generated/images/guide/start/cart-page-full.png' alt="Cart page with products added">
</figure>
@ -429,7 +357,6 @@ StackBlitz tip: Any time the preview refreshes, the cart is cleared. If you make
StackBlitz 提示:只要预览刷新,就会清除购物车。如果你对该应用进行了更改,页面就会刷新,你需要重新购买商品来填充购物车。
</div>
<!--
@ -442,14 +369,12 @@ Learn more: See [Introduction to Services and Dependency Injection](guide/archit
要了解关于[服务](guide/architecture-services "架构>服务简介和 DI")的更多信息,请参阅[“服务和依赖注入简介”](guide/architecture-services "架构>服务简介和 DI")。
</div>
## Retrieve shipping prices
## 检索运费价格
<!-- Accessing data with the HTTP client -->
Data returned from servers often takes the form of a stream.
@ -458,23 +383,19 @@ The Angular HTTP client (`HttpClient`) is a built-in way to fetch data from exte
从服务器返回的数据通常采用流的形式。流是很有用的因为它们可以很容易地转换返回的数据也可以修改请求数据的方式。Angular 的 HTTP 客户端( `HttpClient` )是一种内置的方式,可以从外部 API 中获取数据,并以流的形式提供给你的应用。
In this section, you'll use the HTTP client to retrieve shipping prices from an external file.
在本节中,你将使用 HTTP 客户端从外部文件中检索运费。
### Predefined shipping data
### 预定义的配送数据
For the purpose of this Getting Started guide, we have provided shipping data in `assets/shipping.json`.
You'll use this data to add shipping prices for items in the cart.
为了满足本“入门指南”的需求,我们在 `assets/shipping.json` 中提供了配送数据。你可以利用这些数据为购物车中的商品添加运费。
<code-example header="src/assets/shipping.json" path="getting-started/src/assets/shipping.json">
</code-example>
@ -482,33 +403,27 @@ You'll use this data to add shipping prices for items in the cart.
### 为应用启用 HttpClient
Before you can use Angular's HTTP client, you must set up your app to use `HttpClientModule`.
在使用 Angular 的 HTTP 客户端之前,你必须先设置你的应用来使用 `HttpClientModule`
Angular's `HttpClientModule` registers the providers needed to use a single instance of the `HttpClient` service throughout your app.
The `HttpClient` service is what you inject into your services to fetch data and interact with external APIs and resources.
Angular 的 `HttpClientModule` 中注册了在整个应用中使用 `HttpClient` 服务的单个实例所需的服务提供商。你可以在服务中注入 `HttpClient` 服务来获取数据并与外部 API 和资源进行交互。
1. Open `app.module.ts`.
打开 `app.module.ts`
This file contains imports and functionality that is available to the entire app.
该文件包含可供整个应用使用的导入对象和功能。
1. Import `HttpClientModule` from the `@angular/common/http` package.
`@angular/common/http` 包中导入 `HttpClientModule`
<code-example header="src/app/app.module.ts" path="getting-started/src/app/app.module.ts" region="http-client-module-import">
</code-example>
@ -516,12 +431,10 @@ Angular 的 `HttpClientModule` 中注册了在整个应用中使用 `HttpClient`
`HttpClientModule` 添加到应用模块(`@NgModule`)的 `imports` 数组中。
This registers Angular's `HttpClient` providers globally.
这会在全局注册 Angular 的 `HttpClient` 提供商。
<code-example path="getting-started/src/app/app.module.ts" region="http-client-module">
</code-example>
@ -537,17 +450,14 @@ To do: Should ReactiveFormsModule already be here?
### 为购物车服务启用 HttpClient
1. Open `cart.service.ts`.
打开 `cart.service.ts`
1. Import `HttpClient` from the `@angular/common/http` package.
`@angular/common/http` 包中导入 `HttpClient`
<code-example header="src/app/cart.service.ts" path="getting-started/src/app/cart.service.ts" region="import-http">
</code-example>
@ -555,7 +465,6 @@ To do: Should ReactiveFormsModule already be here?
`HttpClient` 注入到 `CartService` 组件类的构造函数中:
<code-example path="getting-started/src/app/cart.service.ts" region="inject-http">
</code-example>
@ -563,133 +472,92 @@ To do: Should ReactiveFormsModule already be here?
### 定义 get() 方法
As you've seen, multiple components can leverage the same service.
Later in this tutorial, the shipping component will use the cart service to retrieve shipping data via HTTP from the `shipping.json` file.
Here you'll define the `get()` method that will be used.
如你所见,多个组件可以使用同一个服务。在这个教程的后半部分,商品配送组件将使用该购物车服务从 `shipping.json` 文件中借助 HTTP 检索配送数据。在这里你要定义很快就要用到的 `get()` 方法。
1. Continue working in `cart.service.ts`.
继续在 `cart.service.ts` 中工作。
1. Below the `clearCart()` method, define a new `getShippingPrices()` method that uses the `HttpClient#get()` method to retrieve the shipping data (types and prices).
`clearCart()` 方法下面,定义一个新的 `getShippingPrices()` 方法,该方法使用 `HttpClient#get()` 方法检索配送数据(类型和价格)。
<code-example header="src/app/cart.service.ts" path="getting-started/src/app/cart.service.ts" region="get-shipping"></code-example>
<div class="alert is-helpful">
Learn more: See the [HttpClient guide](guide/http "HttpClient guide") for more information about Angular's `HttpClient`.
要了解关于 Angular `HttpClient` 的更多信息,请参阅[HttpClient 指南](guide/http "HttpClient 指南")。
</div>
## Define the shipping page
## 定义配送页面
Now that your app can retrieve shipping data, you'll create a shipping component and associated template.
现在你的应用已经可以检索配送数据了,你还要创建一个配送组件和相关的模板。
1. Generate a new component named `shipping`.
生成一个名为 `shipping` 的新组件。
Reminder: In the file list, right-click the `app` folder, choose `Angular Generator` and `Component`.
提示:在文件列表框中,右键单击 `app` 文件夹,选择 `Angular Generator``Component`
<code-example header="src/app/shipping/shipping.component.ts" path="getting-started/src/app/shipping/shipping.component.1.ts"></code-example>
1. In `app.module.ts`, add a route for shipping. Specify a `path` of `shipping` and a component of `ShippingComponent`.
`app.module.ts` 中,添加一个配送路由。其 `path``shipping`,其 component 为 `ShippingComponent`
<code-example header="src/app/app.module.ts" path="getting-started/src/app/app.module.ts" region="shipping-route"></code-example>
The new shipping component isn't hooked into any other component yet, but you can see it in the preview pane by entering the URL specified by its route. The URL has the pattern: `https://getting-started.stackblitz.io/shipping` where the `getting-started.stackblitz.io` part may be different for your StackBlitz project.
新的配送组件尚未挂钩到任何其它组件,但你可以通过输入其路由特有的 URL 在预览窗格中看到它。该 URL 具有以下模式:`https://getting-started.stackblitz.io/shipping`,其中的 gets-started.stackblitz.io 部分可能与你的 StackBlitz 项目不同。
1. Modify the shipping component so it uses the cart service to retrieve shipping data via HTTP from the `shipping.json` file.
修改配送组件,让它利用购物车服务从 `shipping.json` 文件中通过 HTTP 检索配送数据。
1. Import the cart service.
导入购物车服务。
<code-example header="src/app/shipping/shipping.component.ts" path="getting-started/src/app/shipping/shipping.component.ts" region="imports"></code-example>
1. Define a `shippingCosts` property.
定义 `shippingCosts` 属性。
<code-example path="getting-started/src/app/shipping/shipping.component.ts" region="props"></code-example>
1. Inject the cart service into the `ShippingComponent` class:
把购物车服务注入到 `ShippingComponent` 类中:
<code-example path="getting-started/src/app/shipping/shipping.component.ts" region="inject-cart-service"></code-example>
1. Set the `shippingCosts` property using the `getShippingPrices()` method from cart service.
利用购物车服务的 `getShippingPrices()` 方法设置 `shippingCosts` 属性。
<code-example path="getting-started/src/app/shipping/shipping.component.ts" region="ctor"></code-example>
1. Update the shipping component's template to display the shipping types and prices using async pipe:
利用 async 管道修改配送组件的模板,以显示配送类型和价格:
<code-example header="src/app/shipping/shipping.component.html" path="getting-started/src/app/shipping/shipping.component.html"></code-example>
<!--
To decide: Should we describe async pipe
-->
@ -698,22 +566,16 @@ Now that your app can retrieve shipping data, you'll create a shipping component
在购物车页面中添加一个到配送页面的链接:
<code-example header="src/app/cart/cart.component.html" path="getting-started/src/app/cart/cart.component.2.html"></code-example>
1. Test your shipping prices feature:
测试这个运费价格功能:
Click on the "Checkout" button to see the updated cart. (Remember that changing the app causes the preview to refresh, which empties the cart.)
点击“Checkout”按钮查看更新后的购物车。注意修改应用会导致预览窗格刷新这会清空购物车。
<figure>
<img src='generated/images/guide/start/cart-empty-with-shipping-prices.png' alt="Cart with link to shipping prices">
</figure>
@ -722,7 +584,6 @@ Now that your app can retrieve shipping data, you'll create a shipping component
点击此链接可以导航到运费页。
<figure>
<img src='generated/images/guide/start/shipping-prices.png' alt="Display shipping prices">
</figure>
@ -731,17 +592,14 @@ Now that your app can retrieve shipping data, you'll create a shipping component
## 下一步
Congratulations! You have an online store application with a product catalog and shopping cart. You also have the ability to look up and display shipping prices.
恭喜!你有一个带有商品名录和购物车的在线商店应用了,而且你还可以查询并显示运费。
To continue exploring Angular, choose either of the following options:
要继续探索 Angular请选择下列选项之一
* [Continue to the "Forms" section](start/forms "Getting Started: Forms") to finish the app by adding the shopping cart page and a form-based checkout feature. You'll create a form to collect user information as part of checkout.
[继续浏览“表单”部分](start/forms "入门:表单"),通过添加购物车页面和基于表单的结帐功能来完成该应用。你还可以创建一个表单来收集用户信息,作为结账过程的一部分。
@ -750,4 +608,3 @@ To continue exploring Angular, choose either of the following options:
[跳到“部署”部分,](start/deployment "入门:部署")把你的应用部署到 Firebase 或转成本地开发。

View File

@ -2,26 +2,22 @@
# 部署
To deploy your application, you have to compile it, and then host the JavaScript, CSS, and HTML on a web server. Built Angular applications are very portable and can live in any environment or served by any technology, such as Node, Java, .NET, PHP, and many others.
要部署应用,你必须先编译它,然后在 Web 服务器上托管 JavaScript、CSS 和 HTML。构建后的 Angular 应用程序非常容易移植,它可以在任何环境中运行,也可以用任何技术提供服务,比如 NodeJava.NETPHP 等等。
<div class="alert is-helpful">
Whether you came here directly from [Your First App](start "Getting Started: Your First App"), or completed the entire online store application through the [Routing](start/routing "Getting Started: Routing"), [Managing Data](start/data "Getting Started: Managing Data"), and [Forms](start/forms "Getting Started: Forms") sections, you have an application that you can deploy by following the instructions in this section.
无论你是从[你的第一个应用](start "入门:你的第一个应用")直接来到这里,还是经过[路由](start/routing "入门:路由")、[管理数据](start/data "入门:管理数据")和[表单](start/forms "入门:表单")部分,完成了整个在线商店应用之后来到这里,都可以按照本节中的说明进行部署。
</div>
## Share your application
## 从 StackBlitz 开始部署
StackBlitz projects are public by default, allowing you to share your Angular app via the project URL. Keep in mind that this is a great way to share ideas and prototypes, but it is not intended for production hosting.
StackBlitz 项目默认是公开的,你可以通过项目的 URL 来共享你的应用。记住,虽然这是一种共享思路和原型的良好途径,但并不适合承载产品环境。
@ -46,22 +42,18 @@ StackBlitz 项目默认是公开的,你可以通过项目的 URL 来共享你
## 本地构建
To build your application locally or for production, you will need to download the source code from your StackBlitz project. Click the `Download Project` icon in the left menu across from `Project` to download your files.
要在本地构建应用或未生产环境构建应用,你需要从 StackBlitz 项目中下载源代码。单击左侧菜单中的 `Download Project` 图标以下载文件。
Once you have the source code downloaded and unzipped, use the [Angular Console](https://angularconsole.com "Angular Console web site") to serve the application, or you install Node and have the Angular CLI installed.
下载并解压源代码后,就可以使用 [Angular Console](https://angularconsole.com "Angular Console 的网站") 来启动开发服务器了,也可以先安装 Node 再安装 Angular CLI。
From the terminal, install the Angular CLI globally with:
在终端上,全局安装 Angular CLI
```sh
npm install -g @angular/cli
```
@ -70,12 +62,10 @@ This will install the command `ng` into your system, which is the command you us
这会把命令 `ng` 安装到你的系统中,你可以用它的命令来创建新工作区或新项目、启动开发服务器、或构建那些可以共享或分发的版本。
Create a new Angular CLI workspace using the [`ng new`](cli/new "CLI ng new command reference") command:
[`ng new`](cli/new "在 CLI 中输入新的命令参考") 命令用来创建一个新的 Angular CLI 工作空间:
```sh
ng new my-project-name
```
@ -84,7 +74,6 @@ From there you replace the `/src` folder with the one from your `StackBlitz` dow
进入你从 `StackBlitz` 下载的 `/src` 文件夹,然后执行 build 命令。
```sh
ng build --prod
```
@ -93,27 +82,22 @@ This will produce the files that you need to deploy.
这会产生你要部署的文件。
#### Hosting the built project
#### 托管已构建的项目
The files in the `dist/my-project-name` folder are static and can be hosted on any web server capable of serving files (Node, Java, .NET) or any backend (Firebase, Google Cloud, App Engine, others).
`dist/my-project-name` 文件夹中的文件都是静态的,可以托管在任何能够提供文件服务的 Web 服务器上NodeJava.NET也可以是任何后端FirebaseGoogle CloudApp Engine 等)。
### Hosting an Angular app on Firebase
### 在 Firebase 上托管一个 Angular 应用
One of the easiest ways to get your site live is to host it using Firebase.
要想让你的网站上线,最简单的办法之一就是使用 Firebase 托管它。
1. Sign up for a firebase account on [Firebase](https://firebase.google.com/ "Firebase web site").
在 [Firebase](https://firebase.google.com/ "Firebase 网站") 上注册一个 firebase 账号。
@ -142,34 +126,28 @@ One of the easiest ways to get your site live is to host it using Firebase.
部署之后,访问 <https://your-firebase-project-name.firebaseapp.com> 进行实时查看!
### Hosting an Angular app anywhere else
### 在其它地方托管 Angular 应用
To host an Angular app on another web host, you'll need to upload or send the files to the host.
Because you are building a Single Page Application, you'll also need to make sure you redirect any invalid URLs to your `index.html` file.
Learn more about development and distribution of your application in the [Building & Serving](guide/build "Building and Serving Angular Apps") and [Deployment](guide/deployment "Deployment guide") guides.
要在其它网络主机上托管 Angular 应用,你需要上传文件或把它们发送到那台主机。由于你正在构建一个单页面应用,所以你还要确保把所有无效的 URL 都重定向到 `index.html` 文件。在[构建与服务](guide/build "构建和提供 Angular 应用服务")和[部署](guide/deployment "部署指南")指南”中可以找到关于开发和部署应用的更多信息。
## Join our community
## 加入我们的社区
You are now an Angular developer! [Share this moment](https://twitter.com/intent/tweet?url=https://angular.io/start&text=I%20just%20finished%20the%20Angular%20Getting%20Started%20Tutorial "Angular on Twitter"), tell us what you thought of this Getting Started, or submit [suggestions for future editions](https://github.com/angular/angular/issues/new/choose "Angular GitHub repository new issue form").
你现在是一位 Angular 的开发者了![分享这一刻](https://twitter.com/intent/tweet?url=https://next.angular.io/getting-started&text=I%20just%20finished%20the%20Angular%20Getting%20Started%20Tutorial "Angular on Twitter"),告诉我们你对这份“入门文档”的看法,或者[为今后的版本](https://github.com/angular/angular/issues/new/choose "Angular GitHub 存储库中的新问题表单")提交[建议](https://github.com/angular/angular/issues/new/choose "Angular GitHub 存储库中的新问题表单")。
Angular offers many more capabilities, and you now have a foundation that empowers you to build an application and explore those other capabilities:
Angular 还提供了更多功能,不过你现在已经有了基础,可以让你构建一个应用并探索其它的能力:
* Angular provides advanced capabilities for mobile apps, animation, internationalization, server-side rendering, and more.
Angular 为移动应用、动画、国际化、服务器端渲染等提供了先进的功能。
@ -186,9 +164,7 @@ Angular 还提供了更多功能,不过你现在已经有了基础,可以让
Angular 还拥有广泛的[第三方工具和库](https://angular.io/resources "Angular 资源列表")互助网。
Keep current by following the [Angular blog](https://blog.angular.io/ "Angular blog").
敬请关注 [Angular 官方博客](https://blog.angular.io/ "Angular 的博客")。

View File

@ -2,52 +2,42 @@
# 表单
At the end of [Managing Data](start/data "Getting Started: Managing Data"), the online store application has a product catalog and a shopping cart.
当[管理数据](start/data "入门:管理数据")结束时,这个在线商店应用有了一个商品名录和一个购物车。
In this section, you'll finish the app by adding a form-based checkout feature. You'll create a form to collect user information as part of checkout.
在本节中,你将通过添加基于表单的结帐功能来完成该应用。你还将创建一个表单来收集用户信息,作为结账过程的一部分。
## Forms in Angular
## Angular 中的表单
Forms in Angular take the standard capabilities of the HTML based forms and add an orchestration layer to help with creating custom form controls, and to supply great validation experiences. There are two parts to an Angular Reactive form, the objects that live in the component to store and manage the form, and the visualization of the form that lives in the template.
Angular 中的表单采用了基于 HTML 表单的标准功能并添加了一个编排层以帮助你创建自定义表单控件并提供了出色的验证体验。Angular 响应式表单有两个部分,组件中那些用于存储和管理表单的对象,以及表单在模板中的可视化。
## Define the checkout form model
## 定义结帐的表单模型
First, you'll set up the checkout form model. The form model is the source of truth for the status of the form and is defined in the component class.
首先你要设置一个结账的表单模型。表单模型是表单状态的真相之源source of truth它是在组件类中定义的。
1. Open `cart.component.ts`.
打开 `cart.component.ts`
1. Angular's `FormBuilder` service provides convenient methods for generating controls. As with the other services you've used, you need to import and inject the service before you can use it:
Angular 的 `FormBuilder` 服务为生成控件提供了方便的方法。和你使用过的其它服务一样,你需要导入并注入该服务,然后才能使用它:
1. Import the `FormBuilder` service from the `@angular/forms` package.
`@angular/forms` 包中导入 `FormBuilder` 服务。
<code-example header="src/app/cart/cart.component.ts" path="getting-started/src/app/cart/cart.component.ts" region="imports">
</code-example>
@ -55,12 +45,10 @@ First, you'll set up the checkout form model. The form model is the source of tr
`FormBuilder` 服务是由 `ReactiveFormsModule` 提供的,它已经在之前修改过的 `AppModule` 定义过(在 `app.module.ts` )。
1. Inject the `FormBuilder` service.
注入这个 `FormBuilder` 服务。
<code-example header="src/app/cart/cart.component.ts" path="getting-started/src/app/cart/cart.component.ts" region="inject-form-builder">
</code-example>
@ -68,7 +56,6 @@ First, you'll set up the checkout form model. The form model is the source of tr
`CartComponent` 类中,定义 `checkoutForm` 属性来存储表单模型。
<code-example header="src/app/cart/cart.component.ts" path="getting-started/src/app/cart/cart.component.ts" region="checkout-form">
</code-example>
@ -76,7 +63,6 @@ First, you'll set up the checkout form model. The form model is the source of tr
在结帐时,该应用会提示用户输入姓名和地址。这样你接下来就可以收集到这些信息了。可以使用 `FormBuilder#group()` 方法,用一个包含 `name``address` 字段的表单模型赋值给 `checkoutForm` 属性。
<code-example header="src/app/cart/cart.component.ts" path="getting-started/src/app/cart/cart.component.ts" region="checkout-form-group" linenums="false">
</code-example>
@ -84,17 +70,14 @@ First, you'll set up the checkout form model. The form model is the source of tr
对于结帐过程,用户需要能够提交表单数据(他们的姓名和地址)。在提交订单之后,表单应该重置,购物车应该清空。
In `cart.component.ts`, define an `onSubmit()` method to process the form. Use the `CartService#clearCart()` method to empty the cart items and reset the form after it is submitted. (In a real-world app, this method also would submit the data to an external server.)
`cart.component.ts` 中,定义一个 `onSubmit()` 方法来处理表单。使用 `CartService#clearCart()` 方法清空购物车项目,并在提交完之后重置该表单。(在实际应用中,此方法也会把数据提交给外部服务器。)
The entire cart component is shown below:
整个购物车组件如下所示:
<code-example header="src/app/cart/cart.component.ts" path="getting-started/src/app/cart/cart.component.ts">
</code-example>
@ -102,32 +85,26 @@ The form model is defined in the component class. To reflect the model in the vi
表单模型是在组件类中定义的。要在视图中反映这个模型,你需要创建一个结帐表单。
## Create the checkout form
## 创建结帐表单
Next, you'll add a checkout form at the bottom of the "Cart" page.
接下来,你将在“购物车”页面的底部添加一个结帐表单。
1. Open `cart.component.html`.
打开 `cart.component.html`
1. At the bottom of the template, add an empty HTML form to capture user information.
在模板的底部,添加一个空的 HTML 表单来捕获用户信息。
1. Use a `formGroup` property binding to bind the `checkoutForm` to the `form` tag in the template. Also include a "Purchase" button to submit the form.
使用 `formGroup` 属性绑定把 `checkoutForm` 绑定到模板中的 `form` 标签上。还要提供一个 “Purchase” 按钮来提交表单。
<code-example header="src/app/cart/cart.component.html" path="getting-started/src/app/cart/cart.component.3.html" region="checkout-form">
</code-example>
@ -135,7 +112,6 @@ Next, you'll add a checkout form at the bottom of the "Cart" page.
`form` 标签上,使用 `ngSubmit` 事件绑定来监听表单提交,并使用 `checkoutForm` 值调用 `onSubmit()` 方法。
<code-example path="getting-started/src/app/cart/cart.component.html" region="checkout-form-1">
</code-example>
@ -143,7 +119,6 @@ Next, you'll add a checkout form at the bottom of the "Cart" page.
`name``address` 添加输入字段。使用 `formControlName` 属性绑定来把 `checkoutForm` 表单控件中的 `name``address` 绑定到它们的输入字段。最终的完整版组件如下所示:
<code-example path="getting-started/src/app/cart/cart.component.html" region="checkout-form-2">
</code-example>
@ -151,7 +126,6 @@ After putting a few items in the cart, users can now review their items, enter n
往购物车中放入几件商品之后,用户可以查看这些商品,输入自己的姓名和地址,进行购买:
<figure>
<img src='generated/images/guide/start/cart-with-items-and-form.png' alt="Cart page with checkout form">
</figure>
@ -160,12 +134,10 @@ After putting a few items in the cart, users can now review their items, enter n
## 下一步
Congratulations! You have a complete online store application with a product catalog, a shopping cart, and a checkout function.
恭喜!你有了一个完整的在线商店应用,它具有商品名录,购物车和结账功能。
[Continue to the "Deployment" section](start/deployment "Getting Started: Deployment") to move to local development, or deploy your app to Firebase or your own server.
[继续浏览“部署”部分,](start/deployment "入门:部署")把你的应用转移到本地开发、部署到 Firebase 或你自己的服务器。

View File

@ -2,40 +2,32 @@
# Angular 入门:你的第一个应用
Welcome to Angular!
Angular 欢迎你!
This tutorial introduces you to the essentials of Angular.
It leverages what you already know about HTML and JavaScript&mdash;plus some useful Angular features&mdash;to build a simple online store application, with a catalog, shopping cart, and check-out form.
You don't need to install anything: you'll build the app using the [StackBlitz](https://stackblitz.com/ "StackBlitz web site") online development environment.
本教程将向你介绍 Angular 的基本知识。它利用你已经知道的 HTML 和 JavaScript 以及一些有用的 Angular 特性来构建一个简单的在线商店应用,它具有商品名录、购物车和结账表单功能。你无需安装任何东西:你将使用 [StackBlitz](https://stackblitz.com/ "StackBlitz 网站") 在线开发环境来构建本应用。
<div class="callout is-helpful">
<header>New to web development?</header>
<header>你是 Web 开发的新手吗?</header>
You'll find many resources to complement the Angular docs. Mozilla's MDN docs include both [HTML](https://developer.mozilla.org/en-US/docs/Learn/HTML "Learning HTML: Guides and tutorials") and [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript "JavaScript") introductions. [TypeScript's docs](https://www.typescriptlang.org/docs/home.html "TypeScript documentation") include a 5-minute tutorial. Various online course platforms, such as [Udemy](http://www.udemy.com "Udemy online courses") and [Codecademy](https://www.codecademy.com/ "Codeacademy online courses"), also cover web development basics.
你可以找到很多资源作为 Angular 文档的补充。Mozilla 的 MDN 文档同时包含了 [HTML](https://developer.mozilla.org/en-US/docs/Learn/HTML "学习 HTML指南和教程") 和 [JavaScript 的](https://developer.mozilla.org/en-US/docs/Web/JavaScript "JavaScript") 介绍。[TypeScript 的文档](https://www.typescriptlang.org/docs/home.html "TypeScript 文档")中包含一个 5 分钟教程。各种在线课程平台,比如 [Udemy](http://www.udemy.com "Udemy 在线课程") 和 [Codecademy](https://www.codecademy.com/ "Codeacademy 在线课程"),也涵盖了 Web 开发的一些基础知识。
</div>
{@a new-project}
## Create a new project
## 创建一个新项目
<h4>
<live-example name="getting-started-v0" noDownload>点此在 StackBlitz 上创建一个新项目</live-example>
</h4>
@ -49,7 +41,6 @@ We've seeded this particular app with a top bar&mdash;containing the store name
<img src="generated/images/guide/start/new-app.png" alt="Starter online store app">
</figure>
<div class="callout is-helpful">
<header>StackBlitz 提示</header>
@ -69,7 +60,6 @@ We've seeded this particular app with a top bar&mdash;containing the store name
StackBlitz 正在不断改进,因此生成的代码可能会略有不同,但应用的行为是一样的。
</div>
{@a template-syntax}
@ -77,7 +67,6 @@ We've seeded this particular app with a top bar&mdash;containing the store name
## 模板语法
<!--
Angular extends HTML with a template syntax that gives components control over the display of content.
This section introduces five things you can do in an Angular template to affect what your user sees, based on the component's state and behavior:
@ -88,27 +77,22 @@ In this section, you'll learn about template syntax by enhancing the "Products"
Angular 的模板语法扩展了 HTML 和 JavaScript。在本节中你将通过增强 “商品” 区域来了解模板语法。
(So that you can focus on the template syntax, the following steps use predefined product data and methods from the `product-list.component.ts` file.)
(为了让你专注于模板语法,下列步骤使用了 `product-list.component.ts` 文件中预定义的一些商品数据和方法。)
1. In the `product-list` folder, open the template file `product-list.component.html`.
`product-list` 文件夹中,打开模板文件 `product-list.component.html`
1. Modify the product list template to display a list of product names.
修改商品列表模板,看是否列出了商品名称。
1. We want each product in the list to be displayed the same way, one after the other on the page. To iterate over the predefined list of products, use the `*ngFor` directive. Put the `*ngFor` directive on a `<div>`, as shown below:
我们希望列表中的每个商品能以同样的方式在页面上挨个显示出来。要遍历这些预定义的商品列表,请使用 `*ngFor` 指令。把 `*ngFor` 指令加到 `<div>` 上,如下图所示:
<code-example header="src/app/product-list/product-list.component.html" path="getting-started/src/app/product-list/product-list.component.2.html" region="ngfor">
</code-example>
@ -127,7 +111,6 @@ Angular 的模板语法扩展了 HTML 和 JavaScript。在本节中你将通
要显示商品的名称,请使用插值语法 `{{}}`。插值会把属性的值作为文本渲染出来。在 `<div>` 里面,添加一个 `<h3>` 标题来显示商品 name 属性的插值:
<code-example path="getting-started/src/app/product-list/product-list.component.2.html" region="interpolation">
</code-example>
@ -135,7 +118,6 @@ Angular 的模板语法扩展了 HTML 和 JavaScript。在本节中你将通
预览窗格会立即更新,以显示列表中每个商品的名称。
<figure>
<img src="generated/images/guide/start/template-syntax-product-names.png" alt="Product names added to list">
</figure>
@ -144,7 +126,6 @@ Angular 的模板语法扩展了 HTML 和 JavaScript。在本节中你将通
在最终的应用中,每个商品名称都会链接到商品详情。现在添加这个链接,并使用属性绑定语法 `[]` 把该链接的 title 设置成该商品的名字,如下所示:
<code-example path="getting-started/src/app/product-list/product-list.component.2.html">
</code-example>
@ -156,7 +137,6 @@ Angular 的模板语法扩展了 HTML 和 JavaScript。在本节中你将通
在预览窗格中,将鼠标悬停在显示的商品名称上,可以看到绑定的 name 属性值。它们俩是一样的。插值表达式 `{{}}` 允许你把属性值渲染为文本;而属性绑定语法 `[]` 则允许你在模板表达式中使用属性值。
<figure>
<img src="generated/images/guide/start/template-syntax-product-anchor.png" alt="Product name anchor text is product name property">
</figure>
@ -166,7 +146,6 @@ Angular 的模板语法扩展了 HTML 和 JavaScript。在本节中你将通
添加商品说明。在 p 标签上,使用 `*ngIf` 指令,这样才能在当前商品有描述信息的情况下创建这个 p 元素。
<code-example path="getting-started/src/app/product-list/product-list.component.3.html">
</code-example>
@ -174,7 +153,6 @@ Angular 的模板语法扩展了 HTML 和 JavaScript。在本节中你将通
该应用会立即在列表中显示每种商品的名称和描述,如下所示。请注意,最后一个商品根本没有描述信息。由于该商品的 description 属性为空,因此不会创建 p 元素(包括静态文本 “Description”
<figure>
<img src="generated/images/guide/start/template-syntax-product-description.png" alt="Product descriptions added to list">
</figure>
@ -183,7 +161,6 @@ Angular 的模板语法扩展了 HTML 和 JavaScript。在本节中你将通
添加一个按钮,以便让用户可与朋友分享商品。把 button 的 `click` 事件绑定到我们替你定义好的 `share()` 事件上(位于 `product-list.component.ts` )。事件绑定是通过把事件名称包裹在圆括号 `()` 中完成的,如下所示:
<code-example path="getting-started/src/app/product-list/product-list.component.4.html">
</code-example>
@ -191,7 +168,6 @@ Angular 的模板语法扩展了 HTML 和 JavaScript。在本节中你将通
现在,每个商品都有一个 “Share” 按钮了:
<figure>
<img src="generated/images/guide/start/template-syntax-product-share-button.png" alt="Share button added for each product">
</figure>
@ -200,7 +176,6 @@ Angular 的模板语法扩展了 HTML 和 JavaScript。在本节中你将通
测试 “Share” 按钮:
<figure>
<img src="generated/images/guide/start/template-syntax-product-share-alert.png" alt="Alert box indicating product has been shared">
</figure>
@ -210,7 +185,6 @@ In the process, you've learned to use five common features of Angular's template
该应用现在具有商品列表和共享功能。在这个过程中,你已经学会了 Angular 模板语法的五个常用特性:
* `*ngFor`
* `*ngIf`
* Interpolation `{{ }}`
@ -225,34 +199,28 @@ In the process, you've learned to use five common features of Angular's template
事件绑定 `()`
<div class="alert is-helpful">
Learn more: See the [Template Syntax guide](guide/template-syntax "Template Syntax") for information about the full capabilities of Angular's template syntax.
要了解关于 Angular 模板语法全部特性的信息,请参阅[模板语法指南](guide/template-syntax "模板语法")。
</div>
{@a components}
## Components
## 组件
*Components* define areas of responsibility in your UI that let you reuse these sets of UI functionality.
You've already built one with the product list component.
*组件*在 UI 中定义了一些责任区,让你能重用这些 UI 功能集。你已经通过商品列表组件构建了一个。
A component is comprised of three things:
组件包含三部分:
* **A component class,** which handles data and functionality. In the previous section, the product data and the `share()` method were defined for you in the component class.
**一个组件类**,它用来处理数据和功能。上一节,我们在组件类中定义了商品数据和 `share()` 方法。
@ -265,7 +233,6 @@ A component is comprised of three things:
**组件专属的样式**定义了外观和感觉。商品列表中还没有定义任何样式。
<!--
### Class definition
@ -288,12 +255,10 @@ An Angular application is composed of a tree of components, in which each Angula
Angular 应用程序由一棵组件树组成,每个 Angular 组件都有一个明确的用途和责任。
Currently, our app has three components:
目前,我们的应用有三个组件:
<figure>
<img src="generated/images/guide/start/app-components.png" alt="Online store with three components">
</figure>
@ -310,50 +275,41 @@ Currently, our app has three components:
`app-product-list`(紫色框)是你在上一节中修改过的商品列表。
In the next section, you'll expand the app's capabilities by adding a new component for a product alert. You'll add it as a child of the product list component.
在下一节中,你将通过为商品提醒添加新组件来扩展应用的功能。你可以把它添加为商品列表组件的子组件。
<div class="alert is-helpful">
Learn more: See [Introduction to Components](guide/architecture-components "Architecture > Introduction to Components") for more information about components and how they interact with templates.
要了解关于组件及其与模板交互的更多信息,请参阅[“组件简介”](guide/architecture-components "架构>组件简介")。
</div>
{@a input}
## Input
## 输入
Currently, the product list displays the name and description of each product.
You might have noticed that the product list component also defines a `products` property that contains imported data for each product. (See the `products` array in `products.ts`.)
目前,商品列表会显示每个商品的名称和描述。你可能已经注意到,商品列表组件还定义了一个 `products` 属性,它包含每个商品的导入数据。(参见 `products.ts` 中的 `products` 数组。)
We're going to create a new alert feature. The alert feature will take a product as an input. It will then check the product's price, and, if the price is greater than $700, it will display a "Notify Me" button that lets users sign up for notifications when the product goes on sale.
我们要创建一个新的提醒功能。提醒功能会接收一个商品作为输入。然后它会检查商品的价格,如果价格高于 700 美元它会显示一个“Notify Me”通知我按钮让用户注册一个当商品上市时发送的通知。
1. Create a new product alerts component.
创建一个新商品提醒组件。
1. Right click on the `app` folder and use the `Angular Generator` to generate a new component named `product-alerts`.
右键单击 `app` 文件夹,使用 `Angular Generator` 生成一个名为 `product-alerts` 的新组件。
<figure>
<img src="generated/images/guide/start/generate-component.png" alt="StackBlitz command to generate component">
</figure>
@ -362,7 +318,6 @@ We're going to create a new alert feature. The alert feature will take a product
该 generator 为组件的三个部分创建了启动文件:
* `product-alerts.component.ts`
* `product-alerts.component.html`
* `product-alerts.component.css`
@ -371,193 +326,145 @@ We're going to create a new alert feature. The alert feature will take a product
打开 `product-alerts.component.ts`
<code-example header="src/app/product-alerts/product-alerts.component.ts" path="getting-started/src/app/product-alerts/product-alerts.component.1.ts" region="as-generated"></code-example>
1. Notice the `@Component` decorator. This indicates that the following class is a component. It provides metadata about the component, including its templates, styles, and a selector.
注意 `@Component` 装饰器。这表明它下面的类是一个组件。它提供了有关该组件的元数据,包括它的模板、样式和选择器。
* The `selector` is used to identify the component. The selector is the name you give the Angular component when it is rendered as an HTML element on the page. By convention, Angular component selectors begin with the prefix `app-`, followed by the component name.
`selector` 用于标识该组件。该选择器是当 Angular 组件在页面中渲染出 HTML 元素时使用的名字。按照惯例Angular 组件选择器会以前缀 `app-` 开头,后跟组件名称。
* The template and style filenames. These reference the other two files generated for you.
模板和样式文件名。它们是对另外两个生成的文件的引用。
1. The component definition also includes an exported class (`ProductAlertsComponent`), which handles functionality for the component.
组件定义中还包含一个导出类( `ProductAlertsComponent` ),用于处理该组件的功能。
1. Set up the new product alerts component to receive a product as input:
设置新商品提醒组件,让它接收一个商品作为输入:
1. Import `Input` from `@angular/core`.
`@angular/core` 导入 `Input`
<code-example path="getting-started/src/app/product-alerts/product-alerts.component.1.ts" region="imports"></code-example>
1. In the `ProductAlertsComponent` class definition, define a property named `product` with an `@Input` decorator. The `@Input` decorator indicates that the property value will be passed in from the component's parent (in this case, the product list component).
`ProductAlertsComponent` 类的定义中,定义一个带 `@Input` 装饰器的 `product` 属性。`@Input` 装饰器指出其属性值是从组件的父组件(在本例中就是商品列表组件)中传入的。
<code-example path="getting-started/src/app/product-alerts/product-alerts.component.1.ts" region="input-decorator"></code-example>
1. Define the view for the new product alert component.
定义这个新商品提醒组件的视图。
Open the `product-alerts.component.html` template and replace the placeholder paragraph with a "Notify Me" button that appears if the product price is over $700.
打开 `product-alerts.component.html` 模板,把作为占位符的 p 替换为如果商品价格超过 700 美元就要显示出来的“通知我”按钮。
<code-example header="src/app/product-alerts/product-alerts.component.html" path="getting-started/src/app/product-alerts/product-alerts.component.1.html"></code-example>
1. Display the new product alert component as part of (a child of) the product list.
把这个新商品提醒组件显示为该商品列表的一部分(子组件)。
1. Open `product-list.component.html`.
打开 `product-list.component.html`
1. To include the new component, use its selector (`app-product-alert`) as you would an HTML element.
要包含这个新组件,只要像使用 HTML 元素一样使用它的选择器( `app-product-alert` )就可以了。
1. Pass the current product as input to the component using property binding.
通过属性绑定把当前商品作为输入传递给组件。
<code-example header="src/app/product-list/product-list.component.html" path="getting-started/src/app/product-list/product-list.component.5.html" region="app-product-alerts"></code-example>
The new product alert component takes a product as input from the product list. With that input, it shows or hides the "Notify Me" button, based on the price of the product. The Phone XL price is over $700, so the "Notify Me" button appears on that product.
新商品提醒组件会从商品列表中获取商品作为输入信息。通过该输入,它会根据商品的价格显示或隐藏 “Notify Me” 按钮。由于 Phone XL 的售价超过了 700 美元所以该商品上会出现“Notify Me”按钮。
<figure>
<img src="generated/images/guide/start/product-alert-button.png" alt="Product alert button added to products over $700">
</figure>
<div class="alert is-helpful">
Learn more: See [Component Interaction](guide/component-interaction "Components & Templates > Component Interaction") for more information about passing data from a parent to child component, intercepting and acting upon a value from the parent, and detecting and acting on changes to input property values.
要了解如何将数据从父组件传递到子组件、拦截并处理来自父组件的值,以及检测并对输入属性值进行更改的更多信息,请参阅 [组件交互](guide/component-interaction "组件和模板>组件交互")一章。
</div>
{@a output}
## Output
## 输出
The "Notify Me" button doesn't do anything yet. In this section, you'll set up the product alert component so that it emits an event up to the product list component when the user clicks "Notify Me". You'll define the notification behavior in the product list component.
“Notify Me” 按钮暂时还没有任何操作。在本节中你将设置商品提醒组件以便当用户单击“Notify Me”时它会向该商品列表组件发出事件。你将在商品列表组件中定义通知行为。
1. Open `product-alerts.component.ts`.
打开 `product-alerts.component.ts`
1. Import `Output` and `EventEmitter` from `@angular/core`:
`@angular/core` 中导入 `Output``EventEmitter`
<code-example header="src/app/product-alerts/product-alerts.component.ts" path="getting-started/src/app/product-alerts/product-alerts.component.ts" region="imports"></code-example>
1. In the component class, define a property named `notify` with an `@Output` decorator and an instance of event emitter. This makes it possible for the product alert component to emit an event when the value of the notify property changes.
在组件类中,用 `@Output` 装饰器和一个事件发射器EventEmitter实例定义一个名为 `notify` 的属性。这可以让商品提醒组件在 notify 属性发生变化时发出事件。
<code-example path="getting-started/src/app/product-alerts/product-alerts.component.ts" region="input-output"></code-example>
1. In the product alert template (`product-alerts.component.html`), update the "Notify Me" button with an event binding to call the `notify.emit()` method.
在商品提醒模板(`product-alerts.component.html`用事件绑定更新“Notify Me”按钮以调用 `notify.emit()` 方法。
<code-example header="src/app/product-alerts/product-alerts.component.html" path="getting-started/src/app/product-alerts/product-alerts.component.html"></code-example>
1. Next, define the behavior that should happen when the button is clicked. Recall that it's the parent (product list component)&mdash;not the product alerts component&mdash;that's going to take the action. In the `product-list.component.ts` file, define an `onNotify()` method, similar to the `share()` method:
接下来,定义单击该按钮时应该发生的行为。回想一下,应该由父组件(商品列表组件)采取行动,而不是商品提醒组件。在 `product-list.component.ts` 文件中,定义一个 `onNotify()` 方法,类似于 `share()` 方法:
<code-example header="src/app/product-list/product-list.component.ts" path="getting-started/src/app/product-list/product-list.component.ts" region="on-notify"></code-example>
1. Finally, update the product list component to receive output from the product alerts component.
最后,修改商品列表组件以接收商品提醒组件的输出。
In `product-list.component.html`, bind the `app-product-alerts` component (which is what displays the "Notify Me" button) to the `onNotify()` method of the product list component.
`product-list.component.html` 中,把 `app-product-alerts` 组件就是它显示的“Notify Me”按钮`notify` 事件绑定到商品列表组件的 `onNotify()` 方法。
<code-example header="src/app/product-list/product-list.component.html" path="getting-started/src/app/product-list/product-list.component.6.html" region="on-notify"></code-example>
1. Try out the "Notify Me" button:
试试“Notify Me”按钮
<figure>
<img src="generated/images/guide/start/product-alert-notification.png" alt="Product alert notification confirmation dialog">
</figure>
<div class="alert is-helpful">
Learn more: See [Component Interaction](guide/component-interaction "Components & Templates > Component Interaction") for more information about listening for events from child components, reading child properties or invoking child methods, and using a service for bi-directional communication within the family.
要了解关于从子组件侦听事件、读取子属性或调用子方法以及如何用服务在组件家族中进行双向通信的详细信息,请参阅“[组件交互](guide/component-interaction "组件和模板>组件交互")”一章。
</div>
{@a next-steps}
@ -565,24 +472,20 @@ Learn more: See [Component Interaction](guide/component-interaction "Components
## 下一步
Congratulations! You've completed your first Angular app!
恭喜!你已经完成了第一个 Angular 应用!
You have a basic online store catalog, with a product list, "Share" button, and "Notify Me" button.
You've learned about the foundation of Angular: components and template syntax.
You've also learned how the component class and template interact, and how components communicate with each other.
你有了一个基本的在线商店目录它带有商品列表“Share”按钮和“Notify Me”按钮。你已经了解了 Angular 的基础知识:组件和模板语法。你还学习了组件类和模板如何交互,以及组件如何相互通信。
To continue exploring Angular, choose either of the following options:
要继续探索 Angular请选择以下选项之一
* [Continue to the "Routing" section](start/routing "Getting Started: Routing") to create a product details page that can be accessed by clicking a product name and that has its own URL pattern.
[继续浏览“路由”部分](start/routing "入门:路由"),创建一个商品详情页面,通过单击商品名称,可以访问该页面,该页面有自己的 URL 模式。

View File

@ -2,26 +2,22 @@
# 路由
At the end of [Your First App](start "Getting Started: Your First App"), the online store application has a basic product catalog.
The app doesn't have any variable states or navigation.
There is one URL, and that URL always displays the "My Store" page with a list of products and their descriptions.
在[你的第一个应用](start "入门:你的第一个应用")结束时,这个在线商店应用会有一个基本的商品名录。该应用还没有任何可变的状态或导航。它只有一个 URL该 URL 总是会显示“我的商店”页面,其中是商品列表及其描述。
In this section, you'll extend the app to display full product details in separate pages, with their own URLs.
在本节中,你将扩展本应用以便在单独的页面中显示完整的商品详情,并让它使用自己的 URL。
To do this, you'll use the Angular *router*.
The Angular [router](guide/glossary#router "router definition") enables you to show different components and data to the user based on where the user is in the application.
The router enables navigation from one view to the next as users perform application tasks:
为此,你要使用 Angular 的*路由器*。借助 Angular [路由器](guide/glossary#router "router definition"),你可以根据用户在应用中的位置向用户显示不同的组件和数据。当用户执行应用任务时,路由器可以从一个视图导航到另一个视图。
* Enter a URL in the address bar, and the browser navigates to a corresponding page.
在地址栏中输入一个 URL浏览器就会导航到相应的页面。
@ -34,32 +30,26 @@ The router enables navigation from one view to the next as users perform applica
点击浏览器的后退和前进按钮,浏览器会在你看过的页面历史中前后导航。
## Registering a route
## 注册路由
The app is already set up to use the Angular router and to use routing to navigate to the product list component you modified earlier. Let's define a route to show individual product details.
该应用已经设置为使用 Angular 路由器,并通过路由导航到之前修改过的商品列表组件。让我们来定义一个可以显示商品详情的路由。
1. Generate a new component for product details. Give the component the name `product-details`.
为商品详情生成一个新组件。把组件命名为 `product-details`
Reminder: In the file list, right-click the `app` folder, choose `Angular Generator` and `Component`.
提示:在文件列表框中,右键单击 `app` 文件夹,选择 `Angular Generator``Component`
1. In `app.module.ts`, add a route for product details, with a `path` of `products/:productId` and `ProductDetailsComponent` for the `component`.
`app.module.ts` 中,添加一个商品详情路由,该路由的 `path``products/:productId``component``ProductDetailsComponent`
<code-example header="src/app/app.module.ts" path="getting-started/src/app/app.module.ts" region="product-details-route">
</code-example>
@ -67,33 +57,27 @@ The app is already set up to use the Angular router and to use routing to naviga
路由会将一个或多个 URL 路径与一个组件关联起来。
1. Define a link using the `RouterLink` directive. The `routerLink` defines how the user navigates to the route (or URL) declaratively
in the component template.
使用 `RouterLink` 指令定义一个链接。`routerLink` 定义了如何在组件模板中声明式的导航到指定路由(或 URL
We want the user to click a product name to display the details for that product.
我们希望用户点击商品名称来显示该商品的详情。
1. Open `product-list.component.html`.
打开 `product-list.component.html`
1. Update the `*ngFor` directive to assign each index in the `products` array to the `productId` variable when iterating over the list.
修改 `*ngFor` 指令,在遍历列表的过程中把 `products` 数组中的每个索引赋值给 `productId` 变量。
1. Modify the product name anchor to include a `routerLink`.
修改商品名称的链接,使其包含 `routerLink`
<code-example header="src/app/product-list/product-list.component.html" path="getting-started/src/app/product-list/product-list.component.html" region="router-link">
</code-example>
@ -105,17 +89,14 @@ The app is already set up to use the Angular router and to use routing to naviga
RouterLink 指令让路由器控制了一个链接元素。在这种情况下路由URL包含一个固定的区段 `/products` ),但其最后一个区段是变量,要插入当前商品的 id 属性。例如,`id` 为 1 的商品的 URL 类似于 `https://getting-started-myfork.stackblitz.io/products/1`
1. Test the router by clicking a product name. The app displays the product details component, which currently always says "product-details works!" (We'll fix this in the next section.)
通过单击商品名称来测试路由器。该应用会显示商品详情组件,该组件目前始终显示 “product-details works” (我们将在下一节修复此问题。)
Notice that the URL in the preview window changes. The final segment is `products/1`.
注意,预览窗口中的 URL 会发生变化。其最后一段是 `products/1`
<figure>
<img src="generated/images/guide/start/product-details-works.png" alt="Product details page with updated URL">
</figure>
@ -124,27 +105,22 @@ The app is already set up to use the Angular router and to use routing to naviga
## 使用路由信息
The product details component handles the display of each product. The Angular Router displays components based on the browser's URL and your defined routes. You'll use the Angular Router to combine the `products` data and route information to display the specific details for each product.
商品详情组件负责处理每个商品的显示。Angular 的路由器会根据浏览器的 URL 和你定义的这些路由来决定如何显示组件。你可以通过 Angular 的路由器来组合使用 `products` 数据和路由信息,以显示每个商品的详情。
1. Open `product-details.component.ts`
打开 `product-details.component.ts` 文件
1. Arrange to use product data from an external file.
改用外部文件中的商品数据。
1. Import `ActivatedRoute` from the `@angular/router` package, and the `products` array from `../products`.
`@angular/router` 包导入 `ActivatedRoute`,从 `../products` 文件导入 `products` 数组。
<code-example header="src/app/product-details/product-details.component.ts" path="getting-started/src/app/product-details/product-details.component.1.ts" region="imports">
</code-example>
@ -152,7 +128,6 @@ The product details component handles the display of each product. The Angular R
定义 `product` 属性,并把 `ActivatedRoute` 注入到构造函数中。
<code-example header="src/app/product-details/product-details.component.ts" path="getting-started/src/app/product-details/product-details.component.1.ts" region="props-methods">
</code-example>
@ -161,7 +136,6 @@ The product details component handles the display of each product. The Angular R
`ActivatedRoute` 专门用于由 Angular 路由器加载的每个路由组件。它包含关于该路由,路由参数以及与该路由关联的其它数据的信息。
<!--
To do: This is the first time we inject anything into a component. Should we mention it here? There's also a comment about maybe explaining it a bit in the services section (in data.md).
-->
@ -170,7 +144,6 @@ The product details component handles the display of each product. The Angular R
`ngOnInit()` 方法中,*订阅subscribe*路由参数并根据其 `productId` 获取商品信息。
<code-example path="getting-started/src/app/product-details/product-details.component.1.ts" region="get-product">
</code-example>
@ -179,12 +152,10 @@ The product details component handles the display of each product. The Angular R
这个路由参数对应于路由中定义的路径变量。`productId` 是从与该路由匹配的 URL 中提供的。你可以通过 `productId` 来显示每个单独商品的详细信息。
1. Update the template to display product details information inside an `*ngIf`.
修改模板,在 `*ngIf` 中显示商品详情。
<code-example header="src/app/product-details/product-details.component.html" path="getting-started/src/app/product-details/product-details.component.html" region="details">
</code-example>
@ -192,7 +163,6 @@ Now, when the user clicks on a name in the product list, the router navigates yo
现在,当用户点击商品列表中的某个名字时,路由器就会导航到商品的不同网址,用商品详情组件代替商品列表组件,并显示商品详情。
<figure>
<img src="generated/images/guide/start/product-details-routed.png" alt="Product details page with updated URL and full details displayed">
</figure>
@ -203,19 +173,16 @@ Learn more: See [Routing & Navigation](guide/router "Routing & Navigation") for
要了解关于 Angular 路由器的更多信息,请参阅[路由和导航](guide/router "路由与导航")。
</div>
## Next steps
## 下一步
Congratulations! You have integrated routing into your online store.
恭喜!你已经把路由集成到你的在线商店了。
* Products are linked from the product list page to individual products
从商品列表页面链接到了单个商品
@ -224,12 +191,10 @@ Congratulations! You have integrated routing into your online store.
用户可以点击列表中的某个商品名称来在新视图中查看其详细信息,并带有显著的 URL路由
To continue exploring Angular, choose either of the following options:
要继续探索 Angular请选择以下选项之一
* [Continue to the "Managing Data" section](start/data "Getting Started: Managing Data") to add the shopping cart feature, using a service to manage the cart data and using HTTP to retrieve external data for shipping prices.
[继续浏览“管理数据”部分](start/data "入门:管理数据"),以添加购物车功能,使用服务来管理购物车数据,并通过 HTTP 检索配送价格的外部数据。