From c1b8183c381df1c9f3ab9edc422b981896e88293 Mon Sep 17 00:00:00 2001 From: Zhicheng Wang Date: Wed, 15 Jun 2016 23:21:05 +0800 Subject: [PATCH] Add some blank lines, to avoid unnecessary conflicts on merging. --- public/docs/ts/latest/tutorial/toh-pt5.jade | 213 ++++++++++++++++++++ 1 file changed, 213 insertions(+) diff --git a/public/docs/ts/latest/tutorial/toh-pt5.jade b/public/docs/ts/latest/tutorial/toh-pt5.jade index 49e8086186..5bd8e5a252 100644 --- a/public/docs/ts/latest/tutorial/toh-pt5.jade +++ b/public/docs/ts/latest/tutorial/toh-pt5.jade @@ -2,28 +2,40 @@ include ../_util-fns :marked # Routing Around the App + # 应用中的路由 + We received new requirements for our Tour of Heroes application: 我们收到了一份《英雄指南》的新需求: + * Add a *Dashboard* view. + * 添加一个*仪表盘*视图。 + * Navigate between the *Heroes* and *Dashboard* views. + * 在*英雄列表*和*仪表盘*视图之间导航。 + * Clicking on a hero in either view navigates to a detail view of the selected hero. + * 无论在哪个视图中点击一个英雄,都会导航到该英雄的详情页。 + * Clicking a *deep link* in an email opens the detail view for a particular hero; + * 在邮件中点击一个*深链接*,会直接打开一个特定英雄的详情视图。 When we’re done, users will be able to navigate the app like this: 完成时,用户就能像这样浏览一个应用: + figure.image-display img(src='/resources/images/devguide/toh/nav-diagram.png' alt="查看导航") :marked We'll add Angular’s *Component Router* to our app to satisfy these requirements. 我们将把Angular*组件路由器*加入应用中,以满足这些需求。(译注:硬件领域中的路由器是用来帮你找到另一台网络设备的,而这里的路由器用于帮你找到一个组件) + .l-sub-section :marked The [Routing and Navigation](../guide/router-deprecated.html) chapter covers the router in more detail @@ -42,14 +54,18 @@ p 运行这部分的#[+liveExampleLink2('在线例子', 'toh-5')]。 pop out the preview window by clicking the blue 'X' button in the upper right corner: 注意看浏览器地址栏中的URL变化,点击右上角的蓝色'X'按钮,弹出预览窗口。 + .l-main-section :marked ## Where We Left Off + ## 我们在哪儿 + Before we continue with our Tour of Heroes, let’s verify that we have the following structure after adding our hero service and hero detail component. If not, we’ll need to go back and follow the previous chapters. 在继续《英雄指南》之前,先来检查一下,在添加了英雄服务和英雄详情组件之后,你是否已经有了如下目录结构。如果没有,你得先回上一章,再照做一遍。 + .filetree .file angular2-tour-of-heroes .children @@ -71,6 +87,7 @@ p 运行这部分的#[+liveExampleLink2('在线例子', 'toh-5')]。 .file typings.json :marked ### Keep the app transpiling and running + ### 让应用代码保持转译和运行 Open a terminal/console window and enter the following command to @@ -95,14 +112,23 @@ code-example(language="bash"). 下面是我们的计划: * Turn `AppComponent` into an application shell that only handles navigation + * 把`AppComponent`变成应用程序的“壳”,它只处理导航, + * Relocate the *Heroes* concerns within the current `AppComponent` to a separate `HeroesComponent` + * 把现在由`AppComponent`关注的*英雄们*移到一个独立的`HeroesComponent`中, + * Add routing + * 添加路由 + * Create a new `DashboardComponent` + * 添加一个新的`DashboardComponent`组件 + * Tie the *Dashboard* into the navigation structure + * 把*仪表盘*加入导航结构中。 .l-sub-section @@ -114,6 +140,7 @@ code-example(language="bash"). .l-main-section :marked ## Splitting the *AppComponent* + ## 拆分*AppComponent* Our current app loads `AppComponent` and immediately displays the list of heroes. @@ -142,11 +169,17 @@ code-example(language="bash"). The steps are to rename: 改名的步骤如下: + * `app.component.ts` file to `heroes.component.ts` + * 把`app.component.ts`文件改名为`heroes.component.ts` + * `AppComponent` class to `HeroesComponent` + * 把`AppComponent`类改名为`HeroesComponent` + * Selector `my-app` to `my-heroes` + * 把`my-app`选择器改名为`my-heroes` :marked @@ -154,7 +187,9 @@ code-example(language="bash"). :marked ## Create *AppComponent* + ## 创建*AppComponent* + The new `AppComponent` will be the application shell. It will have some navigation links at the top and a display area below for the pages we navigate to. @@ -166,29 +201,49 @@ code-example(language="bash"). 这些起始步骤是: * create a new file named `app.component.ts`. + * 创建一个名叫`app.component.ts`的新文件。 + * define an `AppComponent` class. + * 定义一个`AppComponent`类。 + * `export` it so we can reference it during bootstrapping in `main.ts`. + * `export`它,以便我们能在`main.ts`的启动期间引用它。 + * expose an application `title` property. + * 暴露应用的`title`属性。 + * add the `@Component` metadata decorator above the class with a `my-app` selector. + * 在类的上方添加`@Component`元数据装饰器,装饰器中带有`my-app`选择器。 + * add a template with `

` tags surrounding a binding to the `title` property. + * 在模板中添加一个`

`标签,包裹着到`title`属性的绑定。 + * add the `` tags to the template so we still see the heroes. + * 在模板中添加``标签,以便我们仍能看到英雄列表。 + * add the `HeroesComponent` to the `directives` array so Angular recognizes the `` tags. + * 添加`HeroesComponent`组件到`directives`数组中,以便Angular能认识``标签。 + * add the `HeroService` to the `providers` array because we'll need it in every other view. + * 添加`HeroService`到`providers`数组中,因为我们的每一个视图都需要它。 + * add the supporting `import` statements. + * 添加支持性的`import`语句。 Our first draft looks like this: 我们的第一个草稿就像这样: + +makeExample('toh-5/ts/app/app.component.1.ts', null, 'app/app.component.ts (v1)') :marked .callout.is-critical @@ -202,6 +257,7 @@ code-example(language="bash"). 回到`HeroesComponent`,并从`providers`数组中**移除`HeroService`**。 我们要把它从`HeroesComponent`*提升*到`AppComponent`中。 我们可不希望在应用的两个不同层次上存在它的***两个副本***。 + :marked The app still runs and still displays heroes. Our refactoring of `AppComponent` into a new `AppComponent` and a `HeroesComponent` worked! @@ -210,8 +266,10 @@ code-example(language="bash"). 应用仍然在运行,并显示着英雄列表。 我们把`AppComponent`重构成了一个新的`AppComponent`和`HeroesComponent`,它们工作得很好! 我们毫发无损的完成了这次重构。 + :marked ## Add Routing + ## 添加路由 We're ready to take the next step. @@ -227,21 +285,31 @@ code-example(language="bash"). 我们需要Angular的*组件路由器*。 ### Set the base tag + ### 设置base标签 + Open the `index.html` and add `` at the top of the `` section. 打开`index.html`并且在``区的顶部添加``语句。 + +makeExample('toh-5/ts/index.html', 'base-href', 'index.html (base href)')(format=".") + .callout.is-important + header base href is essential + header base href是不可或缺的 + :marked See the *base href* section of the [Router](../guide/router-deprecated.html#!#base-href) chapter to learn why this matters. 查看[路由器](../guide/router-deprecated.html#!#base-href)一章的*base href*部分,了解为何如此。 + :marked ### Make the router available. + ### 让路由可用。 + The *Component Router* is a service. Like any service, we have to import it and make it available to the application by adding it to the `providers` array. @@ -253,6 +321,7 @@ code-example(language="bash"). Angular路由器是由多个服务(`ROUTER_PROVIDERS`)和多个指令(`ROUTER_DIRECTIVES`)以及一个配置装饰器(`RouteConfig`)组成的。我们一次性导入它们。 +makeExample('toh-5/ts/app/app.component.2.ts', 'import-router', 'app/app.component.ts (router imports)')(format=".") + :marked Next we update the `directives` and `providers` metadata arrays to *include* the router assets. @@ -268,6 +337,7 @@ code-example(language="bash"). 我们马上也会从模板中移除``。 ### Add and configure the router + ### 添加与配置路由器 The `AppComponent` doesn't have a router yet. We'll use the `@RouteConfig` decorator to simultaneously @@ -313,8 +383,10 @@ code-example(language="bash"). Learn more about defining routes with @RouteConfig in the [Routing](../guide/router-deprecated.html) chapter. 要学习更多使用`@RouteConfig`定义路由的知识,请参见[路由](../guide/router-deprecated.html)一章。 + :marked ### Router Outlet + ### 路由插座(Outlet) If we paste the path, `/heroes`, into the browser address bar, @@ -333,7 +405,9 @@ code-example(language="bash"). 当我们在应用中导航时,路由器就把激活的组件显示在``里面。 ### Router Links + ### 路由器链接 + We don't really expect users to paste a route URL into the address bar. We add an anchor tag to the template which, when clicked, triggers navigation to the `HeroesComponent`. @@ -358,15 +432,18 @@ code-example(language="bash"). 我们通过一个*链接参数数组*定义了一个*路由说明*。 在这个小例子中,该数组只有一个元素,一个放在引号中的**路由名称**,作为路标。 回来看路由配置表,我们清楚的看到,这个名称 —— `'Heroes'`就是指向`HeroesComponent`的那个路由的名称。 + .l-sub-section :marked Learn about the *link parameters array* in the [Routing](../guide/router-deprecated.html#link-parameters-array) chapter. 学习关于 *链接参数数组* 的更多知识,参见[路由](../guide/router-deprecated.html#link-parameters-array)一章。 + :marked Refresh the browser. We see only the app title. We don't see the heroes list. 刷新浏览器。我们只看到了应用标题。英雄列表到哪里去了? + .l-sub-section :marked The browser's address bar shows `/`. @@ -377,6 +454,7 @@ code-example(language="bash"). 浏览器的地址栏显示的是`/`。而到`HeroesComponent`的路由中的路径是`/heroes`,不是`/`。 我们没有任何路由能匹配当前的路径`/`,所以,自然没啥可显示的。 接下来,我们就修复这个问题。 + :marked We click the "Heroes" navigation link, the browser bar updates to `/heroes`, and now we see the list of heroes. We are navigating at last! @@ -386,6 +464,7 @@ code-example(language="bash"). At this stage, our `AppComponent` looks like this. 在这个阶段,`AppComponent`看起来是这样的: + +makeExample('toh-5/ts/app/app.component.2.ts',null, 'app/app.component.ts (v2)') :marked The *AppComponent* is now attached to a router and displaying routed views. @@ -397,7 +476,9 @@ code-example(language="bash"). :marked ## Add a *Dashboard* + ## 添加一个*仪表盘* + Routing only makes sense when we have multiple views. We need another view. 当我们有多个视图的时候,路由才有意义。所以我们需要另一个视图。 @@ -405,6 +486,7 @@ code-example(language="bash"). Create a placeholder `DashboardComponent` that gives us something to navigate to and from. 先创建一个`DashboardComponent`的占位符,让我们可以导航到它或从它导航出来。 + +makeExample('toh-5/ts/app/dashboard.component.1.ts',null, 'app/dashboard.component.ts (v1)')(format=".") :marked We’ll come back and make it more useful later. @@ -412,7 +494,9 @@ code-example(language="bash"). 我们先不实现它,稍后,我们再回来,给这个组件一些实际用途。 ### Configure the dashboard route + ### 配置仪表盘路由 + Go back to `app.component.ts` and teach it to navigate to the dashboard. 回到`app.component.ts`文件,我们得教它如何导航到这个仪表盘。 @@ -442,6 +526,7 @@ code-example(language="bash"). router will display the dashboard when the browser URL doesn't match an existing route. 幸运的是,我们可以把`useAsDefault: true`属性添加到*路由定义*上。这样,如果浏览器中的URL匹配不上任何已知的路由,路由器就会显示这个仪表盘组件。 + :marked Finally, add a dashboard navigation link to the template, just above the *Heroes* link. @@ -455,6 +540,7 @@ code-example(language="bash"). 我们在`