From b6b3322e7880376f5a65b114dcf1e342d8145ab7 Mon Sep 17 00:00:00 2001 From: Zhicheng WANG Date: Sat, 8 Jun 2019 17:59:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=88=97=E8=A1=A8=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aio/content/start/data.md | 143 -------------------------------- aio/content/start/deployment.md | 24 ------ aio/content/start/forms.md | 28 ------- aio/content/start/index.md | 97 ---------------------- aio/content/start/routing.md | 35 -------- 5 files changed, 327 deletions(-) diff --git a/aio/content/start/data.md b/aio/content/start/data.md index b11bf224f5..d1fb2e7347 100644 --- a/aio/content/start/data.md +++ b/aio/content/start/data.md @@ -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”按钮。你还可以设置一个购物车服务来存储购物车中商品的相关信息。 -
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 "入门:表单")部分,也会从用户的结账页面中访问这个 购物车服务。 -
{@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`。 - - - - 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` 属性来把当前商品的列表(数组)存储在购物车中。 - - - - 1. Define methods to add items to the cart, return cart items, and clear the cart items: 定义把商品添加到购物车、返回购物车商品以及清除购物车商品的方法: - - - - @@ -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. 导入购物车服务。 - @@ -162,7 +131,6 @@ When the "Buy" button is clicked, you'll use the cart service to add the current 注入购物车服务。 - @@ -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 显示一条 "商品已添加到购物车" 的消息 - - - - 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()` 方法: - @@ -220,7 +179,6 @@ When the "Buy" button is clicked, you'll use the cart service to add the current 要查看新的“Buy”按钮,请刷新应用并单击商品名称以显示其详细信息。 -
Display details for selected product with a Buy button
@@ -229,7 +187,6 @@ When the "Buy" button is clicked, you'll use the cart service to add the current 点击“Buy”按钮。该商品已添加到了购物车中存储的商品列表中,并显示一条消息。 -
Display details for selected product with a Buy button
@@ -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`。 - - - - 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` : - @@ -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`。) -
Display cart page before customizing
@@ -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`。 - @@ -355,7 +291,6 @@ Services can be used to share data across components: 注入 `CartService` 来管理购物车信息。 - @@ -363,7 +298,6 @@ Services can be used to share data across components: 定义 `items` 属性,以便把商品存放在购物车中。 - @@ -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` 类是这样的: - @@ -384,12 +316,10 @@ Services can be used to share data across components: 修改模板,加上标题(“Cart”),用带有 `*ngFor` 的 `
` 来显示每个购物车商品的名字和价格。 - The resulting `CartComponent` template should look like this: 生成的 `CartComponent` 模板如下: - @@ -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”返回商品列表。重复上述步骤。 -
Cart page with products added
@@ -429,7 +357,6 @@ StackBlitz tip: Any time the preview refreshes, the cart is cleared. If you make StackBlitz 提示:只要预览刷新,就会清除购物车。如果你对该应用进行了更改,页面就会刷新,你需要重新购买商品来填充购物车。 -
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` 中提供了配送数据。你可以利用这些数据为购物车中的商品添加运费。 - @@ -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`。 - @@ -516,12 +431,10 @@ Angular 的 `HttpClientModule` 中注册了在整个应用中使用 `HttpClient` 把 `HttpClientModule` 添加到应用模块(`@NgModule`)的 `imports` 数组中。 - This registers Angular's `HttpClient` providers globally. 这会在全局注册 Angular 的 `HttpClient` 提供商。 - @@ -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`。 - @@ -555,7 +465,6 @@ To do: Should ReactiveFormsModule already be here? 把 `HttpClient` 注入到 `CartService` 组件类的构造函数中: - @@ -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()` 方法检索配送数据(类型和价格)。 - - - -
Learn more: See the [HttpClient guide](guide/http "HttpClient guide") for more information about Angular's `HttpClient`. 要了解关于 Angular `HttpClient` 的更多信息,请参阅[HttpClient 指南](guide/http "HttpClient 指南")。 -
## 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`。 - - - - 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`。 - - - - 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. 导入购物车服务。 - - - - 1. Define a `shippingCosts` property. 定义 `shippingCosts` 属性。 - - - - 1. Inject the cart service into the `ShippingComponent` class: 把购物车服务注入到 `ShippingComponent` 类中: - - - - 1. Set the `shippingCosts` property using the `getShippingPrices()` method from cart service. 利用购物车服务的 `getShippingPrices()` 方法设置 `shippingCosts` 属性。 - - - - 1. Update the shipping component's template to display the shipping types and prices using async pipe: 利用 async 管道修改配送组件的模板,以显示配送类型和价格: - - - - @@ -698,22 +566,16 @@ Now that your app can retrieve shipping data, you'll create a shipping component 在购物车页面中添加一个到配送页面的链接: - - - - 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”按钮,查看更新后的购物车。(注意,修改应用会导致预览窗格刷新,这会清空购物车。) -
Cart with link to shipping prices
@@ -722,7 +584,6 @@ Now that your app can retrieve shipping data, you'll create a shipping component 点击此链接可以导航到运费页。 -
Display shipping prices
@@ -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 或转成本地开发。 - diff --git a/aio/content/start/deployment.md b/aio/content/start/deployment.md index ccc7992636..460312d1a2 100644 --- a/aio/content/start/deployment.md +++ b/aio/content/start/deployment.md @@ -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 应用程序非常容易移植,它可以在任何环境中运行,也可以用任何技术提供服务,比如 Node,Java,.NET,PHP 等等。 -
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 "入门:表单")部分,完成了整个在线商店应用之后来到这里,都可以按照本节中的说明进行部署。 -
## 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 服务器上(Node,Java,.NET),也可以是任何后端(Firebase,Google Cloud,App 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. 部署之后,访问 进行实时查看! - ### 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 的博客")。 - diff --git a/aio/content/start/forms.md b/aio/content/start/forms.md index ec8f949a9b..8d45e5edbd 100644 --- a/aio/content/start/forms.md +++ b/aio/content/start/forms.md @@ -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` 服务。 - @@ -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` 服务。 - @@ -68,7 +56,6 @@ First, you'll set up the checkout form model. The form model is the source of tr 在 `CartComponent` 类中,定义 `checkoutForm` 属性来存储表单模型。 - @@ -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` 属性。 - @@ -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: 整个购物车组件如下所示: - @@ -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” 按钮来提交表单。 - @@ -135,7 +112,6 @@ Next, you'll add a checkout form at the bottom of the "Cart" page. 在 `form` 标签上,使用 `ngSubmit` 事件绑定来监听表单提交,并使用 `checkoutForm` 值调用 `onSubmit()` 方法。 - @@ -143,7 +119,6 @@ Next, you'll add a checkout form at the bottom of the "Cart" page. 为 `name` 和 `address` 添加输入字段。使用 `formControlName` 属性绑定来把 `checkoutForm` 表单控件中的 `name` 和 `address` 绑定到它们的输入字段。最终的完整版组件如下所示: - @@ -151,7 +126,6 @@ After putting a few items in the cart, users can now review their items, enter n 往购物车中放入几件商品之后,用户可以查看这些商品,输入自己的姓名和地址,进行购买: -
Cart page with checkout form
@@ -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 或你自己的服务器。 diff --git a/aio/content/start/index.md b/aio/content/start/index.md index d8fb5c852e..3c68391e0f 100644 --- a/aio/content/start/index.md +++ b/aio/content/start/index.md @@ -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—plus some useful Angular features—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 网站") 在线开发环境来构建本应用。 -
New to web development?
你是 Web 开发的新手吗?
- 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 开发的一些基础知识。 -
- - {@a new-project} ## Create a new project ## 创建一个新项目 -

点此在 StackBlitz 上创建一个新项目

@@ -49,7 +41,6 @@ We've seeded this particular app with a top bar—containing the store name Starter online store app -
StackBlitz 提示
@@ -69,7 +60,6 @@ We've seeded this particular app with a top bar—containing the store name StackBlitz 正在不断改进,因此生成的代码可能会略有不同,但应用的行为是一样的。 -
{@a template-syntax} @@ -77,7 +67,6 @@ We've seeded this particular app with a top bar—containing the store name ## 模板语法 - @@ -170,7 +144,6 @@ The product details component handles the display of each product. The Angular R 在 `ngOnInit()` 方法中,*订阅(subscribe)*路由参数并根据其 `productId` 获取商品信息。 - @@ -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` 中显示商品详情。 - @@ -192,7 +163,6 @@ Now, when the user clicks on a name in the product list, the router navigates yo 现在,当用户点击商品列表中的某个名字时,路由器就会导航到商品的不同网址,用商品详情组件代替商品列表组件,并显示商品详情。 -
Product details page with updated URL and full details displayed
@@ -203,19 +173,16 @@ Learn more: See [Routing & Navigation](guide/router "Routing & Navigation") for 要了解关于 Angular 路由器的更多信息,请参阅[路由和导航](guide/router "路由与导航")。 - ## 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 检索配送价格的外部数据。