2016-08-17 13:31:40 -07:00
|
|
|
|
- var _example = 'toh-5';
|
|
|
|
|
|
|
|
|
|
block includes
|
|
|
|
|
include ../_util-fns
|
|
|
|
|
- var _appRoutingTsVsAppComp = 'app.routing.ts'
|
|
|
|
|
- var _declsVsDirectives = 'declarations'
|
|
|
|
|
- var _RoutesVsAtRouteConfig = 'Routes'
|
|
|
|
|
- var _RouterModuleVsRouterDirectives = 'RouterModule'
|
|
|
|
|
- var _redirectTo = 'redirectTo'
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
|
|
|
|
:marked
|
|
|
|
|
# Routing Around the App
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
# 应用中的路由
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-19 00:20:38 -04:00
|
|
|
|
We received new requirements for our Tour of Heroes application:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
我们收到了一份《英雄指南》的新需求:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-10 09:37:33 -07:00
|
|
|
|
* Add a *Dashboard* view.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
* 添加一个*仪表盘*视图。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-10 09:37:33 -07:00
|
|
|
|
* Navigate between the *Heroes* and *Dashboard* views.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
* 在*英雄列表*和*仪表盘*视图之间导航。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-10 09:37:33 -07:00
|
|
|
|
* Clicking on a hero in either view navigates to a detail view of the selected hero.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
* 无论在哪个视图中点击一个英雄,都会导航到该英雄的详情页。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-19 00:20:38 -04:00
|
|
|
|
* Clicking a *deep link* in an email opens the detail view for a particular hero;
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
* 在邮件中点击一个*深链接*,会直接打开一个特定英雄的详情视图。
|
2016-06-19 00:20:38 -04:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
When we’re done, users will be able to navigate the app like this:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
完成时,用户就能像这样浏览一个应用:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
figure.image-display
|
2016-04-17 23:31:22 +08:00
|
|
|
|
img(src='/resources/images/devguide/toh/nav-diagram.png' alt="查看导航")
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
We'll add Angular’s *Component Router* to our app to satisfy these requirements.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-14 09:26:54 +08:00
|
|
|
|
我们将把Angular*组件路由器*加入应用中,以满足这些需求。(译注:硬件领域中的路由器是用来帮你找到另一台网络设备的,而这里的路由器用于帮你找到一个组件)
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
2016-08-17 13:31:40 -07:00
|
|
|
|
The [Routing and Navigation](../guide/router.html) chapter covers the router
|
|
|
|
|
in more detail than we will in this tutorial.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-07-15 17:30:05 +01:00
|
|
|
|
[路由和导航](../guide/router.html)章节介绍了更多关于路由的细节。
|
|
|
|
|
|
2016-07-03 17:11:17 -07:00
|
|
|
|
:marked
|
|
|
|
|
Run the <live-example></live-example> for this part.
|
2016-07-07 08:15:51 +08:00
|
|
|
|
|
|
|
|
|
运行这部分的<live-example></live-example>。
|
2016-06-08 09:58:16 +08:00
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
+ifDocsFor('ts|js')
|
|
|
|
|
.l-sub-section
|
2016-08-23 14:02:54 +01:00
|
|
|
|
img(src='/resources/images/devguide/plunker-separate-window-button.png' alt="弹出窗口" align="right" style="margin-right:-20px")
|
2016-08-17 13:31:40 -07:00
|
|
|
|
:marked
|
|
|
|
|
To see the URL changes in the browser address bar,
|
|
|
|
|
pop out the preview window by clicking the blue 'X' button in the upper right corner:
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
|
|
|
|
注意看浏览器地址栏中的URL变化,点击右上角的蓝色'X'按钮,弹出预览窗口。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
.l-main-section
|
|
|
|
|
:marked
|
|
|
|
|
## Where We Left Off
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
## 我们在哪儿
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-19 00:20:38 -04:00
|
|
|
|
Before we continue with our Tour of Heroes, let’s verify that we have the following structure after adding our hero service
|
2015-12-23 09:42:57 -08:00
|
|
|
|
and hero detail component. If not, we’ll need to go back and follow the previous chapters.
|
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
在继续《英雄指南》之前,先来检查一下,在添加了英雄服务和英雄详情组件之后,你是否已经有了如下目录结构。如果没有,你得先回上一章,再照做一遍。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
block intro-file-tree
|
|
|
|
|
.filetree
|
|
|
|
|
.file angular2-tour-of-heroes
|
2015-12-23 09:42:57 -08:00
|
|
|
|
.children
|
2016-08-17 13:31:40 -07:00
|
|
|
|
.file app
|
|
|
|
|
.children
|
|
|
|
|
.file app.component.ts
|
|
|
|
|
.file app.module.ts
|
|
|
|
|
.file hero.service.ts
|
|
|
|
|
.file hero.ts
|
|
|
|
|
.file hero-detail.component.ts
|
|
|
|
|
.file main.ts
|
|
|
|
|
.file mock-heroes.ts
|
|
|
|
|
.file node_modules ...
|
|
|
|
|
.file typings ...
|
|
|
|
|
.file index.html
|
|
|
|
|
.file package.json
|
|
|
|
|
.file styles.css
|
|
|
|
|
.file systemjs.config.js
|
|
|
|
|
.file tsconfig.json
|
|
|
|
|
.file typings.json
|
|
|
|
|
|
|
|
|
|
block keep-app-running
|
|
|
|
|
:marked
|
|
|
|
|
### Keep the app transpiling and running
|
|
|
|
|
|
2016-08-23 14:02:54 +01:00
|
|
|
|
### 让应用代码保持转译和运行
|
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
Open a terminal/console window and enter the following command to
|
|
|
|
|
start the TypeScript compiler, start the server, and watch for changes:
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
|
|
|
|
打开terminal/console窗口,运行下列命令启动TypeScript编译器,它会监视文件变更,并启动开发服务器:
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
2016-06-07 15:51:25 -07:00
|
|
|
|
code-example(language="bash").
|
2015-12-23 09:42:57 -08:00
|
|
|
|
npm start
|
|
|
|
|
|
|
|
|
|
:marked
|
|
|
|
|
The application runs and updates automatically as we continue to build the Tour of Heroes.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
我们继续构建《英雄指南》,应用也会保持运行并自动更新。
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
|
|
|
|
## Action plan
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
## 行动计划
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-15 07:46:26 -07:00
|
|
|
|
Here's our plan:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-15 23:10:41 +08:00
|
|
|
|
下面是我们的计划:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-15 07:46:26 -07:00
|
|
|
|
* Turn `AppComponent` into an application shell that only handles navigation
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-14 14:04:30 +08:00
|
|
|
|
* 把`AppComponent`变成应用程序的“壳”,它只处理导航,
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-15 07:46:26 -07:00
|
|
|
|
* Relocate the *Heroes* concerns within the current `AppComponent` to a separate `HeroesComponent`
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-14 14:04:30 +08:00
|
|
|
|
* 把现在由`AppComponent`关注的*英雄们*移到一个独立的`HeroesComponent`中,
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-15 07:46:26 -07:00
|
|
|
|
* Add routing
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-15 23:10:41 +08:00
|
|
|
|
* 添加路由
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-15 07:46:26 -07:00
|
|
|
|
* Create a new `DashboardComponent`
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-15 23:10:41 +08:00
|
|
|
|
* 添加一个新的`DashboardComponent`组件
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-15 07:46:26 -07:00
|
|
|
|
* Tie the *Dashboard* into the navigation structure
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
* 把*仪表盘*加入导航结构中。
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
|
|
|
|
*Routing* is another name for *navigation*. The *router* is the mechanism for navigating from view to view.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
*路由*是导航的另一个名字。*路由器*就是从一个视图导航到另一个视图的机制。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
|
|
|
|
.l-main-section
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
## Splitting the *AppComponent*
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
## 拆分*AppComponent*
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
Our current app loads `AppComponent` and immediately displays the list of heroes.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
现在的应用会加载`AppComponent`组件,并且立刻显示出英雄列表。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
Our revised app should present a shell with a choice of views (*Dashboard* and *Heroes*) and then default to one of them.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
我们修改后的应用将提供一个壳,它会选择*仪表盘*和*英雄列表*视图之一,然后默认显示它。
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
|
|
|
|
The `AppComponent` should only handle navigation.
|
|
|
|
|
Let's move the display of *Heroes* out of `AppComponent` and into its own `HeroesComponent`.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
`AppComponent`组件应该只处理导航。
|
2016-05-12 22:56:12 +08:00
|
|
|
|
我们来把*英雄列表*的显示职责,从`AppComponent`移到`HeroesComponent`组件中。
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
### *HeroesComponent*
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
`AppComponent` is already dedicated to *Heroes*.
|
|
|
|
|
Instead of moving anything out of `AppComponent`, we'll just rename it `HeroesComponent`
|
|
|
|
|
and create a new `AppComponent` shell separately.
|
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
`AppComponent`的职责已经被移交给`HeroesComponent`了。
|
|
|
|
|
与其把`AppComponent`中所有的东西都搬过去,不如索性把它改名为`HeroesComponent`,然后单独创建一个新的`AppComponent`壳。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-15 07:46:26 -07:00
|
|
|
|
The steps are to rename:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-15 23:10:41 +08:00
|
|
|
|
改名的步骤如下:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
* <span ngio-ex>app.component.ts</span> file to <span ngio-ex>heroes.component.ts</span>
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 14:02:54 +01:00
|
|
|
|
* 把<span ngio-ex>app.component.ts</span>文件改名为<span ngio-ex>heroes.component.ts</span>
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-15 07:46:26 -07:00
|
|
|
|
* `AppComponent` class to `HeroesComponent`
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-15 23:10:41 +08:00
|
|
|
|
* 把`AppComponent`类改名为`HeroesComponent`
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-15 07:46:26 -07:00
|
|
|
|
* Selector `my-app` to `my-heroes`
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-15 23:10:41 +08:00
|
|
|
|
* 把`my-app`选择器改名为`my-heroes`
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
+makeExcerpt('app/heroes.component.ts (showing renamings only)', 'renaming')
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
|
|
|
|
:marked
|
|
|
|
|
## Create *AppComponent*
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
## 创建*AppComponent*
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-19 00:20:38 -04:00
|
|
|
|
The new `AppComponent` will be the application shell.
|
2015-12-23 09:42:57 -08:00
|
|
|
|
It will have some navigation links at the top and a display area below for the pages we navigate to.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
新的`AppComponent`将成为应用的“壳”。
|
2016-05-12 22:56:12 +08:00
|
|
|
|
它将在顶部放一些导航链接,并且把我们要导航到的页面放在下面的显示区中。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
The initial steps are:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
这些起始步骤是:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
* add the supporting `import` statements.
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
* 添加支持性的`import`语句。
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
* Create the file <span ngio-ex>app/app.component.ts</span>.
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
|
|
|
|
* 创建一个名叫<span ngio-ex>app.component.ts</span>的新文件。
|
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
* Define an <span if-docs="ts">exported</span> `AppComponent` class.
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
|
|
|
|
* 定义一个<span if-docs="ts">导出的</span> ``AppComponent`类。
|
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
* Add an `@Component` !{_decorator} above the class with a `my-app` selector.
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
|
|
|
|
* 在类的上方添加`@Component`元数据装饰器,装饰器中带有`my-app`选择器。
|
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
* Move the following from `HeroesComponent` to `AppComponent`:
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
|
|
|
|
* 将下面的项目从`HeroesComponent`移到`AppComponent`:
|
2016-08-17 13:31:40 -07:00
|
|
|
|
* `title` class property
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
|
|
|
|
* `title`类属性
|
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
* `@Component` template `<h1>` element, which contains a binding to `title`
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
|
|
|
|
* 在模板中添加一个`<h1>`标签,包裹着到`title`属性的绑定。
|
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
* Add a `<my-heroes>` element to the app template just below the heading so we still see the heroes.
|
2016-08-23 14:02:54 +01:00
|
|
|
|
*
|
|
|
|
|
在模板中添加`<my-heroes>`标签,以便我们仍能看到英雄列表。
|
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
* Add `HeroesComponent` to the `!{_declsVsDirectives}` !{_array} of `!{_AppModuleVsAppComp}` so Angular recognizes the `<my-heroes>` tags.
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
|
|
|
|
* 添加`HeroesComponent`组件到根模块的`declarations`数组中,以便Angular能认识`<my-heroes>`标签。
|
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
* Add `HeroService` to the `providers` !{_array} of `!{_AppModuleVsAppComp}` because we'll need it in every other view.
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
|
|
|
|
* 添加`HeroService`到根模块的`providers`数组中,因为我们的每一个视图都需要它。
|
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
* Remove `HeroService` from the `HeroesComponent` `providers` !{_array} since it has been promoted.
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
|
|
|
|
* 从`HerosComponent`的`providers`数组中移除HeroService`,因为它被提到模块了。
|
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
* Add the supporting `import` statements for `AppComponent`.
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
|
|
|
|
* 导入`AppComponent`。
|
|
|
|
|
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
Our first draft looks like this:
|
2016-08-12 07:57:51 +08:00
|
|
|
|
|
|
|
|
|
我们的第一个草稿版就像这样:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
block app-comp-v1
|
|
|
|
|
+makeTabs(
|
|
|
|
|
`toh-5/ts/app/app.component.1.ts,
|
|
|
|
|
toh-5/ts/app/app.module.1.ts`,
|
|
|
|
|
',',
|
|
|
|
|
`app/app.component.ts (v1),
|
|
|
|
|
app/app.module.ts (v1)`)
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
.callout.is-critical
|
|
|
|
|
header Remove <i>HeroService</i> from the <i>HeroesComponent</i> providers
|
2016-05-12 22:56:12 +08:00
|
|
|
|
header 从<i>HeroesComponent</i>的<code>providers</code>中移除<i>HeroService</i>
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
Go back to the `HeroesComponent` and **remove the `HeroService`** from its `providers` array.
|
2016-08-09 17:38:25 +01:00
|
|
|
|
We are *promoting* this service from the `HeroesComponent` to the root `NgModule`.
|
2015-12-23 09:42:57 -08:00
|
|
|
|
We ***do not want two copies*** of this service at two different levels of our app.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
回到`HeroesComponent`,并从`providers`数组中**移除`HeroService`**。
|
|
|
|
|
我们要把它从`HeroesComponent`*提升*到`AppComponent`中。
|
|
|
|
|
我们可不希望在应用的两个不同层次上存在它的***两个副本***。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-06-19 00:20:38 -04:00
|
|
|
|
The app still runs and still displays heroes.
|
2015-12-23 09:42:57 -08:00
|
|
|
|
Our refactoring of `AppComponent` into a new `AppComponent` and a `HeroesComponent` worked!
|
|
|
|
|
We have done no harm.
|
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
应用仍然在运行,并显示着英雄列表。
|
2016-04-17 23:31:22 +08:00
|
|
|
|
我们把`AppComponent`重构成了一个新的`AppComponent`和`HeroesComponent`,它们工作得很好!
|
2016-05-12 22:56:12 +08:00
|
|
|
|
我们毫发无损的完成了这次重构。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
## Add Routing
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
## 添加路由
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
|
|
|
|
We're ready to take the next step.
|
2015-12-23 09:42:57 -08:00
|
|
|
|
Instead of displaying heroes automatically, we'd like to show them *after* the user clicks a button.
|
|
|
|
|
In other words, we'd like to navigate to the list of heroes.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
我们已准备好开始下一步。
|
2016-05-12 22:56:12 +08:00
|
|
|
|
与其自动显示英雄列表,我们更希望在用户点击按钮之后才显示它。
|
|
|
|
|
换句话说,我们希望“导航”到英雄列表。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
We'll need the Angular *Component Router*.
|
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
我们需要Angular的*组件路由器*。
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
block angular-router
|
|
|
|
|
:marked
|
|
|
|
|
The Angular router is an external, optional Angular NgModule called `RouterModule`.
|
|
|
|
|
The router is a combination of multiple provided services (`RouterModule`),
|
|
|
|
|
multiple directives (`RouterOutlet, RouterLink, RouterLinkActive`),
|
|
|
|
|
and a configuration (`Routes`). We'll configure our routes first.
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
|
|
|
|
Angular路由器是一个可选的外部Angular NgModule,名叫`RouterModule`。
|
|
|
|
|
路由器包含了多种服务(`RouterModule`)、多种指令(`RouterOutlet, RouterLink, RouterLinkActive`)、
|
|
|
|
|
和一套配置(`Routes`)。我们将先配置路由。
|
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
:marked
|
2016-08-23 15:52:39 +01:00
|
|
|
|
### Add the base tag
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
2016-05-03 22:33:20 +08:00
|
|
|
|
### 设置base标签
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
2016-07-01 08:26:45 +08:00
|
|
|
|
Open the `index.html` and add `<base href="/">` at the top of the `<head>` section.
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
2016-05-03 22:33:20 +08:00
|
|
|
|
打开`index.html`并且在`<head>`区的顶部添加`<base href="/">`语句。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
+makeExcerpt('index.html', 'base-href')
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
.callout.is-important
|
|
|
|
|
header base href is essential
|
2016-04-23 14:54:01 +08:00
|
|
|
|
header base href是不可或缺的
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-06-19 00:20:38 -04:00
|
|
|
|
See the *base href* section of the [Router](../guide/router.html#!#base-href) chapter to learn why this matters.
|
2016-05-19 13:33:02 +08:00
|
|
|
|
|
|
|
|
|
查看[路由器](../guide/router-deprecated.html#!#base-href)一章的*base href*部分,了解为何如此。
|
2016-08-17 13:31:40 -07:00
|
|
|
|
a#configure-routes
|
|
|
|
|
block router-config-intro
|
|
|
|
|
:marked
|
|
|
|
|
### Configure routes
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
|
|
|
|
### 配置路由
|
2016-08-12 07:57:51 +08:00
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
Our application doesn't have any routes yet.
|
|
|
|
|
We'll start by creating a configuration file for the application routes.
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
|
|
|
|
本应用还没有路由。我们来为应用的路由新建一个配置文件。
|
2016-06-19 00:20:38 -04:00
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
:marked
|
2016-06-19 00:20:38 -04:00
|
|
|
|
*Routes* tell the router which views to display when a user clicks a link or
|
2016-08-23 14:02:54 +01:00
|
|
|
|
pastes a URL into the browser address bar.
|
2016-08-12 07:57:51 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
*路由*告诉路由器,当用户点击链接或者把URL粘贴到浏览器地址栏时,应该显示哪个视图。
|
2016-04-17 23:31:22 +08:00
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
Let's define our first route as a route to the heroes component:
|
2016-08-12 07:57:51 +08:00
|
|
|
|
|
|
|
|
|
我们的第一个路由是指向`HeroesComponent`的。
|
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
- var _file = _docsFor == 'dart' ? 'app.component.ts' : 'app.routing.ts'
|
|
|
|
|
+makeExcerpt('app/' + _file + ' (heroes route)', 'heroes')
|
|
|
|
|
|
|
|
|
|
- var _are = _docsFor == 'dart' ? 'takes' : 'are'
|
|
|
|
|
- var _routePathPrefix = _docsFor == 'dart' ? '/' : ''
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-08-17 13:31:40 -07:00
|
|
|
|
The `!{_RoutesVsAtRouteConfig}` !{_are} !{_an} !{_array} of *route definitions*.
|
2016-06-19 00:20:38 -04:00
|
|
|
|
We have only one route definition at the moment but rest assured, we'll add more.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-07-01 08:24:23 +08:00
|
|
|
|
这个`RouteConfig`是一个*路由定义*的数组。
|
2016-05-12 22:56:12 +08:00
|
|
|
|
此刻我们只有一个路由定义,但别急,后面还会添加更多。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
This *route definition* has the following parts:
|
2016-07-05 08:34:51 +08:00
|
|
|
|
|
2016-08-23 14:02:54 +01:00
|
|
|
|
“路由定义”包括几个部分:
|
2016-07-05 08:34:51 +08:00
|
|
|
|
|
2016-08-23 14:02:54 +01:00
|
|
|
|
* **path**: the router matches this route's path to the URL in the browser address bar (`!{_routePathPrefix}/heroes`).
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 14:02:54 +01:00
|
|
|
|
* **path**: 路由器会用它来匹配路由中指定的路径和浏览器地址栏中的当前路径,如`!{_routePathPrefix}/heroes`。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
* **component**: the component that the router should create when navigating to this route (`HeroesComponent`).
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
* **component**: 导航到此路由时,路由器需要创建的组件,如`HeroesComponent`。
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
2016-08-17 13:31:40 -07:00
|
|
|
|
Learn more about defining routes with `!{_RoutesVsAtRouteConfig}` in the [Routing](../guide/router.html) chapter.
|
2016-08-09 17:38:25 +01:00
|
|
|
|
|
2016-08-16 10:16:31 +01:00
|
|
|
|
到[路由](../guide/router.html)章节学习更多关于使用路由库来定义路由的知识。
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
+ifDocsFor('ts|js')
|
2016-08-09 17:38:25 +01:00
|
|
|
|
:marked
|
2016-08-23 15:52:39 +01:00
|
|
|
|
We'll export a `routing` constant initialized using the `RouterModule.forRoot` method applied to our !{_array} of routes.
|
|
|
|
|
This method returns a **configured router module** that we'll add to our root NgModule, `AppModule`.
|
2016-08-12 07:57:51 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
使用`RouterModule.forRoot`方法,导出包含了路由数组的`routing`常量。它返回一个**配置好的路由模块**,它将被加入到根NgModule - `AppModule`中。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/app.routing.1.ts (excerpt)', 'routing-export')
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
|
|
|
|
We call the `forRoot` method because we're providing a configured router at the _root_ of the application.
|
|
|
|
|
The `forRoot` method gives us the Router service providers and directives needed for routing.
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
|
|
|
|
调用`forRoot`方法是因为要在应用程序根部提供配置好的路由。
|
|
|
|
|
`forRoot`方法为我们提供了路由需要的服务提供商和指令。
|
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
:marked
|
2016-08-23 14:02:54 +01:00
|
|
|
|
### Make the router available.
|
|
|
|
|
|
|
|
|
|
### 让路由器生效
|
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
We've setup initial routes in the `app.routing.ts` file. Now we'll add it to our root NgModule.
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
|
|
|
|
我们已经在`app.routing.ts`文件中设置好了初始路由,接着把它加到根模块(NgModule)中。
|
2016-08-12 07:57:51 +08:00
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
Import the `routing` constant from `app.routing.ts` and add it the `imports` !{_array} of `AppModule`.
|
2016-08-23 14:02:54 +01:00
|
|
|
|
|
|
|
|
|
我们要从`app.routing.ts`中导入`routing`常量,并把它加入根模块的`imports`数组中。
|
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
+makeExcerpt('app/app.module.ts', 'routing')
|
2016-06-19 00:20:38 -04:00
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
- var _heroesRoute = _docsFor == 'dart' ? "'Heroes'" : 'heroes'
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
### Router Outlet
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-14 09:26:54 +08:00
|
|
|
|
### 路由插座(Outlet)
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
2016-06-19 00:20:38 -04:00
|
|
|
|
If we paste the path, `/heroes`, into the browser address bar,
|
2016-08-17 13:31:40 -07:00
|
|
|
|
the router should match it to the `!{_heroesRoute}` route and display the `HeroesComponent`.
|
2015-12-23 09:42:57 -08:00
|
|
|
|
But where?
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
如果我们把路径`/heroes`粘贴到浏览器的地址栏,路由器会匹配到`'Heroes'`路由,并显示`HeroesComponent`组件。
|
|
|
|
|
但问题是,该把它显示在哪儿呢?
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
We have to ***tell it where*** by adding a `<router-outlet>` element to the bottom of the template.
|
|
|
|
|
`RouterOutlet` is one of the <span if-docs="ts">directives provided by</span> the `!{_RouterModuleVsRouterDirectives}`.
|
2015-12-23 09:42:57 -08:00
|
|
|
|
The router displays each component immediately below the `<router-outlet>` as we navigate through the application.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
我们必须***告诉它位置***,所以我们把`<router-outlet>`标签添加到模板的底部。
|
2016-08-23 15:52:39 +01:00
|
|
|
|
`RouterOutlet`是`!{_RouterModuleVsRouterDirectives}`提供的<span if-docs="ts">指令之一</span>`。
|
2016-05-12 22:56:12 +08:00
|
|
|
|
当我们在应用中导航时,路由器就把激活的组件显示在`<router-outlet>`里面。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
### Router Links
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
### 路由器链接
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
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`.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
我们当然不会真让用户往地址栏中粘贴路由的URL,而应该在模板中的什么地方添加一个a链接标签。点击时,就会导航到`HeroesComponent`组件。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
The revised template looks like this:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
修改过的模板是这样的:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/app.component.1.ts', 'template-v2')
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
block routerLink
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-08-23 15:52:39 +01:00
|
|
|
|
Notice the `routerLink` binding in the anchor tag.
|
|
|
|
|
We bind the `RouterLink` directive (another of the `RouterModule` directives) to a string
|
|
|
|
|
that tells the router where to navigate when the user clicks the link.
|
|
|
|
|
|
|
|
|
|
注意,a标签中的`[routerLink]`绑定。我们把`RouterLink`指令(`ROUTER_DIRECTIVES`中的另一个指令)绑定到一个数组。它将告诉路由器,当用户点击这个链接时,应该导航到哪里。
|
|
|
|
|
|
|
|
|
|
Since our link is not dynamic, we define a *routing instruction* with a **one-time binding** to our route **path**.
|
|
|
|
|
Looking back at the route configuration, we confirm that `'/heroes'` is the path of the route to the `HeroesComponent`.
|
|
|
|
|
|
|
|
|
|
由于这个链接不是动态的,我们只要用**一次性绑定**的方式绑定到路由的路径(path)就行了。
|
|
|
|
|
回来看路由配置表,我们清楚的看到,这个路径 —— `'/heroes'`就是指向`HeroesComponent`的那个路由的路径。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
|
|
|
|
Learn more about dynamic router links and the *link parameters array*
|
|
|
|
|
in the [Routing](../guide/router.html#link-parameters-array) chapter.
|
|
|
|
|
|
|
|
|
|
参阅[路由](../guide/router.html#link-parameters-array)章学习更多动态路由器链接和*链接参数数组*的知识。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-08-23 15:52:39 +01:00
|
|
|
|
Refresh the browser. We see only the app title and heroes link. We don't see the heroes list.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
刷新浏览器。我们只看到了应用标题和英雄链接。英雄列表到哪里去了?
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
2016-06-19 00:20:38 -04:00
|
|
|
|
The browser's address bar shows `/`.
|
|
|
|
|
The route path to `HeroesComponent` is `/heroes`, not `/`.
|
2015-12-23 09:42:57 -08:00
|
|
|
|
We don't have a route that matches the path `/`, so there is nothing to show.
|
|
|
|
|
That's something we'll want to fix.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
浏览器的地址栏显示的是`/`。而到`HeroesComponent`的路由中的路径是`/heroes`,不是`/`。
|
|
|
|
|
我们没有任何路由能匹配当前的路径`/`,所以,自然没啥可显示的。
|
|
|
|
|
接下来,我们就修复这个问题。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-08-17 13:31:40 -07:00
|
|
|
|
We click the *Heroes* navigation link, the browser bar updates to `/heroes`,
|
2015-12-23 09:42:57 -08:00
|
|
|
|
and now we see the list of heroes. We are navigating at last!
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-14 09:26:54 +08:00
|
|
|
|
我们点击“英雄列表(Heroes)”导航链接,浏览器地址栏更新为`/heroes`,并且看到了英雄列表。我们终于导航过去了!
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
|
|
|
|
At this stage, our `AppComponent` looks like this.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
在这个阶段,`AppComponent`看起来是这样的:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExample('app/app.component.1.ts', 'v2', 'app/app.component.ts (v2)')
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
The *AppComponent* is now attached to a router and displaying routed views.
|
|
|
|
|
For this reason and to distinguish it from other kinds of components,
|
|
|
|
|
we call this type of component a *Router Component*.
|
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
*AppComponent*现在加上了路由器,并能显示路由到的视图了。
|
|
|
|
|
因此,为了把它从其它种类的组件中区分出来,我们称这类组件为*路由器组件*。
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
|
|
|
|
:marked
|
|
|
|
|
## Add a *Dashboard*
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
## 添加一个*仪表盘*
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
Routing only makes sense when we have multiple views. We need another view.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
当我们有多个视图的时候,路由才有意义。所以我们需要另一个视图。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
Create a placeholder `DashboardComponent` that gives us something to navigate to and from.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
先创建一个`DashboardComponent`的占位符,让我们可以导航到它或从它导航出来。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/dashboard.component.1.ts (v1)', '')
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
We’ll come back and make it more useful later.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
我们先不实现它,稍后,我们再回来,给这个组件一些实际用途。
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
|
|
|
|
### Configure the dashboard route
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
### 配置仪表盘路由
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
Go back to `!{_appRoutingTsVsAppComp}` and teach it to navigate to the dashboard.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
回到`!{_appRoutingTsVsAppComp}`,我们得教它如何导航到这个仪表盘。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
Import the dashboard component and
|
|
|
|
|
add the following route definition to the `!{_RoutesVsAtRouteConfig}` !{_array} of definitions.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
先导入`DashboardComponent`类,然后把下列路由的定义添加到`!{_RoutesVsAtRouteConfig}`数组中。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
- var _file = _docsFor == 'dart' ? 'lib/app_component.dart' : 'app/app.routing.ts'
|
|
|
|
|
+makeExcerpt(_file + ' (Dashboard route)', 'dashboard')
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+ifDocsFor('ts|js')
|
|
|
|
|
:marked
|
|
|
|
|
Also import and add `DashboardComponent` to our root NgModule's `declarations`.
|
|
|
|
|
|
|
|
|
|
还得把`DashboardComponent`添加到根模块的`declarations`数组中。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/app.module.ts', 'dashboard')
|
2016-08-09 17:38:25 +01:00
|
|
|
|
|
|
|
|
|
:marked
|
2016-08-23 15:52:39 +01:00
|
|
|
|
#### !{_redirectTo}
|
2016-08-12 07:57:51 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
#### !{_redirectTo}
|
2016-06-19 00:20:38 -04:00
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
We want the app to show the dashboard when it starts and
|
|
|
|
|
we want to see a nice URL in the browser address bar that says `/dashboard`.
|
|
|
|
|
Remember that the browser launches with `/` in the address bar.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
我们希望在应用启动的时候就显示仪表盘,而且我们希望在浏览器的地址栏看到一个好看的URL,比如`/dashboard`。
|
|
|
|
|
记住,浏览器启动时,在地址栏中使用的路径是`/`。
|
|
|
|
|
我们可以使用一个重定向路由来达到目的。
|
|
|
|
|
|
|
|
|
|
block redirect-vs-use-as-default
|
|
|
|
|
:marked
|
|
|
|
|
We can use a redirect route to make this happen. Add the following
|
|
|
|
|
to our array of route definitions:
|
|
|
|
|
|
|
|
|
|
可以使用重定向路由来实现它。添加下面的内容到路由定义的数组中:
|
|
|
|
|
|
|
|
|
|
+makeExcerpt('app/app.routing.ts','redirect')
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
|
|
|
|
Learn about the *redirects* in the [Routing](../guide/router.html#!#redirect) chapter.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-24 14:25:47 +01:00
|
|
|
|
要学习关于*重定向*的更多知识,参见[路由与导航](../guide/router.html#!#redirect)一章。
|
2016-06-19 00:20:38 -04:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-08-17 13:31:40 -07:00
|
|
|
|
#### Add navigation to the template
|
2016-08-23 15:52:39 +01:00
|
|
|
|
|
|
|
|
|
#### 添加导航到模版中
|
2016-08-17 13:31:40 -07:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
Finally, add a dashboard navigation link to the template, just above the *Heroes* link.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
最后,在模板上添加一个到仪表盘的导航链接,就放在*英雄(Heroes)*链接的上方。
|
2016-08-23 15:52:39 +01:00
|
|
|
|
|
|
|
|
|
- var _vers = _docsFor == 'dart' ? '' : '.1'
|
|
|
|
|
+makeExcerpt('app/app.component' + _vers + '.ts', 'template-v3')
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
2016-06-19 00:20:38 -04:00
|
|
|
|
We nestled the two links within `<nav>` tags.
|
2015-12-23 09:42:57 -08:00
|
|
|
|
They don't do anything yet but they'll be convenient when we style the links a little later in the chapter.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
我们在`<nav>`标签中放了两个链接。
|
|
|
|
|
它们现在还没有作用,但稍后,当我们对这些链接添加样式时,会显得比较方便。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-08-17 13:31:40 -07:00
|
|
|
|
To see these changes in your browser, go to the application root (`/`) and reload.
|
|
|
|
|
The app displays the dashboard and we can navigate between the dashboard and the heroes.
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
刷新浏览器。应用显示出了仪表盘,并可以在仪表盘和英雄列表之间导航了。
|
2015-12-23 09:42:57 -08:00
|
|
|
|
## Dashboard Top Heroes
|
2016-05-12 22:56:12 +08:00
|
|
|
|
## 仪表盘上的顶级英雄
|
2015-12-23 09:42:57 -08:00
|
|
|
|
Let’s spice up the dashboard by displaying the top four heroes at a glance.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
我们想让仪表盘更有趣,比如:一眼就能看到四个顶级英雄。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
Replace the `template` metadata with a `templateUrl` property that points to a new
|
2016-03-08 13:45:29 +01:00
|
|
|
|
template file.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
把元数据中的`template`属性替换为`templateUrl`属性,它将指向一个新的模板文件。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/dashboard.component.ts', 'templateUrl')
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2016-03-08 13:45:29 +01:00
|
|
|
|
.l-sub-section
|
2016-08-23 15:52:39 +01:00
|
|
|
|
block templateUrl-path-resolution
|
|
|
|
|
:marked
|
|
|
|
|
We specify the path _all the way back to the application root_ —
|
|
|
|
|
<span if-docs="ts">`app/` in this case —</span>
|
|
|
|
|
because Angular doesn't support relative paths _by default_.
|
|
|
|
|
We _can_ switch to [component-relative paths](../cookbook/component-relative-paths.html) if we prefer.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
我们指定的所有路径_都是相对于该应用的根目录 - <span if-docs="ts">这里是`app/`</span>。
|
|
|
|
|
因为Angular_默认_不支持使用相对于当前模块的路径。
|
|
|
|
|
只要喜欢,我们也_可以_切换成[相对于组件的路径](../cookbook/component-relative-paths.html)模式。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-03-08 13:45:29 +01:00
|
|
|
|
:marked
|
2016-08-23 15:52:39 +01:00
|
|
|
|
Create that file with this content:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
使用下列内容创建文件:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/dashboard.component.html')
|
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-06-19 00:20:38 -04:00
|
|
|
|
We use `*ngFor` once again to iterate over a list of heroes and display their names.
|
2015-12-23 09:42:57 -08:00
|
|
|
|
We added extra `<div>` elements to help with styling later in this chapter.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
我们又一次使用`*ngFor`来在英雄列表上迭代,并显示他们的名字。
|
|
|
|
|
还添加了一个额外的`<div>`元素,来帮助稍后的美化工作。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
|
|
|
|
There's a `(click)` binding to a `gotoDetail` method we haven't written yet and
|
2015-12-23 09:42:57 -08:00
|
|
|
|
we're displaying a list of heroes that we don't have.
|
|
|
|
|
We have work to do, starting with those heroes.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
这里的`(click)`绑定到了`gotoDetail`方法,但我们还没实现它。而且我们也还没有要显示的英雄列表数据。
|
|
|
|
|
我们有活儿干了,就从这些英雄列表开始吧。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
### Share the *HeroService*
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
### 共享*HeroService*
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
We'd like to re-use the `HeroService` to populate the component's `heroes` !{_array}.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
我们想要复用`HeroService`来存放组件的`heroes`数组。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
Recall earlier in the chapter that we removed the `HeroService` from the `providers` !{_array} of `HeroesComponent`
|
|
|
|
|
and added it to the `providers` !{_array} of `!{_AppModuleVsAppComp}`.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
回忆一下,在前面的章节中,我们从`HeroesComponent`的`providers`数组中移除了`HeroService`服务,并把它添加到顶级组件`!{_AppModuleVsAppComp}`的`providers`数组中。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
|
|
|
|
That move created a singleton `HeroService` instance, available to *all* components of the application.
|
2016-06-10 09:37:33 -07:00
|
|
|
|
Angular will inject `HeroService` and we'll use it here in the `DashboardComponent`.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
这个改动创建了一个`HeroService`的单例对象,它对应用中的*所有*组件都有效。
|
2016-06-14 14:04:30 +08:00
|
|
|
|
在`DashboardComponent`组件中,Angular会把`HeroService`注入进来,我们就能在`DashboardComponent`中使用它了。
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
|
|
|
|
### Get heroes
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
### 获取英雄数组
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
Open <span ngio-ex>dashboard.component.ts</span> and add the requisite `import` statements.
|
|
|
|
|
|
|
|
|
|
打开<span ngio-ex>dashboard.component.ts</span>文件,并把必备的`import`语句加进去。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/dashboard.component.2.ts','imports')
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
We need `OnInit` interface because we'll initialize the heroes in the `ngOnInit` method as we've done before.
|
|
|
|
|
We need the `Hero` and `HeroService` symbols in order to reference those types.
|
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
我们得实现`OnInit`接口,因为我们要在`ngOnInit`方法中初始化英雄数组 —— 就像上次一样。
|
2016-05-12 22:56:12 +08:00
|
|
|
|
我们需要导入`Hero`类和`HeroService`类来引用它们的数据类型。
|
2016-04-17 23:31:22 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
Now implement the `DashboardComponent` class like this:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
我们现在就实现`DashboardComponent`类,像这样:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/dashboard.component.2.ts (class)', 'component')
|
2016-08-09 17:38:25 +01:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-08-23 15:52:39 +01:00
|
|
|
|
We've seen this kind of logic before in the `HeroesComponent`:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
在`HeroesComponent`之前,我们也看到过类似的逻辑:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
* Define a `heroes` !{_array} property.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
* 创建一个`heroes`数组属性
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
* Inject the `HeroService` in the constructor and hold it in a private `!{_priv}heroService` field.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
* 把`HeroService`注入构造函数中,并且把它保存在一个私有的`!{_priv}heroService`字段中。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
* Call the service to get heroes inside the Angular `ngOnInit` lifecycle hook.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-07-15 17:30:05 +01:00
|
|
|
|
* 在Angular的`ngOnInit`生命周期钩子里面调用服务来获得英雄列表。
|
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
The noteworthy differences: we cherry-pick four heroes (2nd, 3rd, 4th, and 5th)
|
2015-12-23 09:42:57 -08:00
|
|
|
|
and stub the `gotoDetail` method until we're ready to implement it.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
值得注意的区别是:我们提取了四个英雄(第2、3、4、5个),暂时隔离`gotoDetail`方法直到时机成熟。
|
2016-07-15 17:30:05 +01:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
Refresh the browser and see four heroes in the new dashboard.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
刷新浏览器,在这个新的仪表盘中就看到了四个英雄。
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
2016-06-19 00:20:38 -04:00
|
|
|
|
.l-main-section
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
## Navigate to Hero Details
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
## 导航到英雄详情
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
|
|
|
|
Although we display the details of a selected hero at the bottom of the `HeroesComponent`,
|
|
|
|
|
we don't yet *navigate* to the `HeroDetailComponent` in the three ways specified in our requirements:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
虽然我们在`HeroesComponent`组件的底部显示了所选英雄的详情,但我们还从没有*导航*到`HeroDetailComponent`组件过 —— 我们曾在需求中指定过三种:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-20 20:07:39 +08:00
|
|
|
|
1. from the *Dashboard* to a selected hero.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
1. 从*仪表盘(Dashboard)*导航到一个选定的英雄。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-20 20:07:39 +08:00
|
|
|
|
1. from the *Heroes* list to a selected hero.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
1. 从*英雄列表(Heroes)*导航到一个选定的英雄。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-20 20:07:39 +08:00
|
|
|
|
1. from a "deep link" URL pasted into the browser address bar.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
1. 把一个指向该英雄的“深链接”URL粘贴到浏览器的地址栏。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
Adding a hero-detail route seems like an obvious place to start.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
添加`hero-detail`路由,是一个显而易见的起点。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
### Routing to a hero detail
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
### 路由到一个英雄详情
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
We'll add a route to the `HeroDetailComponent` in `!{_appRoutingTsVsAppComp}` where our other routes are configured.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
我们将在`!{_appRoutingTsVsAppComp}`中添加一个到`HeroDetailComponent`的路由,也就是配置其它路由的地方。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
|
|
|
|
The new route is a bit unusual in that we must tell the `HeroDetailComponent` *which hero to show*.
|
2015-12-23 09:42:57 -08:00
|
|
|
|
We didn't have to tell the `HeroesComponent` or the `DashboardComponent` anything.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
新路由的不寻常之处在于,我们必须告诉`HeroDetailComponent`*该显示哪个英雄*。
|
|
|
|
|
以前的`HeroesComponent`组件和`DashboardComponent`组件还从未要求我们告诉它任何东西。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
At the moment the parent `HeroesComponent` sets the component's `hero` property to a
|
|
|
|
|
hero object with a binding like this.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
现在,父组件`HeroesComponent`通过数据绑定来把一个英雄对象设置为组件的`hero`属性。就像这样:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
code-example(language="html").
|
2015-12-23 09:42:57 -08:00
|
|
|
|
<my-hero-detail [hero]="selectedHero"></my-hero-detail>
|
2016-08-17 13:31:40 -07:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-06-19 00:20:38 -04:00
|
|
|
|
That clearly won't work in any of our routing scenarios.
|
2015-12-23 09:42:57 -08:00
|
|
|
|
Certainly not the last one; we can't embed an entire hero object in the URL! Nor would we want to.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
显然,在我们的任何一个路由场景中它都无法工作。
|
2016-05-12 22:56:12 +08:00
|
|
|
|
不仅如此,我们也没法把一个完整的hero对象嵌入到URL中!不过我们本来也不想这样。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
### Parameterized route
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
### 参数化路由
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
We *can* add the hero's `id` to the URL. When routing to the hero whose `id` is 11,
|
|
|
|
|
we could expect to see an URL such as this:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
我们*可以*把英雄的`id`添加到URL中。当导航到一个`id`为11的英雄时,我们期望的URL是这样的:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
code-example(format='').
|
|
|
|
|
/detail/11
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
The `/detail/` part of that URL is constant. The trailing numeric `id` part changes from hero to hero.
|
|
|
|
|
We need to represent that variable part of the route with a *parameter* (or *token*) that stands for the hero's `id`.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
URL中的`/detail/`部分是固定不变的,但结尾的数字`id`部分会随着英雄的不同而变化。
|
|
|
|
|
我们要把路由中可变的那部分表示成一个*参数(parameter)*或*Token*,用以获取英雄的`id`。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
### Configure a Route with a Parameter
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
### 配置带参数的路由
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
Here's the *route definition* we'll use.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
下面是我们将使用的*路由定义*。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
- var _file = _docsFor == 'dart' ? 'app/app.component.ts' : 'app/app.routing.ts'
|
|
|
|
|
+makeExcerpt(_file + ' (hero detail)','hero-detail')
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-06-19 00:20:38 -04:00
|
|
|
|
The colon (:) in the path indicates that `:id` is a placeholder to be filled with a specific hero `id`
|
2015-12-23 09:42:57 -08:00
|
|
|
|
when navigating to the `HeroDetailComponent`.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-14 09:26:54 +08:00
|
|
|
|
路径中的冒号(:)表示`:id`是一个占位符,当导航到这个`HeroDetailComponent`组件时,它将被填入一个特定英雄的`id`。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+ifDocsFor('dart')
|
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
|
|
|
|
Remember to import the hero detail component before creating this route.
|
|
|
|
|
|
|
|
|
|
记得在创建这个路由前导入英雄详情组件。
|
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-06-19 00:20:38 -04:00
|
|
|
|
We're finished with the application routes.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-07-01 08:24:23 +08:00
|
|
|
|
我们已经做好了该应用的路由。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-19 00:20:38 -04:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
We won't add a `'Hero Detail'` link to the template because users
|
|
|
|
|
don't click a navigation *link* to view a particular hero.
|
|
|
|
|
They click a *hero* whether that hero is displayed on the dashboard or in the heroes list.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
我们没有往模板中添加一个`'英雄详情'`,这是因为用户不会直接点击导航栏中的链接去查看一个特定的英雄。
|
2016-05-12 22:56:12 +08:00
|
|
|
|
他们只会通过在英雄列表或者仪表盘中点击来显示一个英雄。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
We'll get to those *hero* clicks later in the chapter.
|
|
|
|
|
There's no point in working on them until the `HeroDetailComponent`
|
|
|
|
|
is ready to be navigated *to*.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
稍后我们会响应这些*英雄*的点击事件。
|
|
|
|
|
现在对它们做什么都还没有意义 —— 除非`HeroDetailComponent`已经做好了,并且能够被导航过去。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
That will require an `HeroDetailComponent` overhaul.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
那就需要对`HeroDetailComponent`做一次大修。
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
2016-06-19 00:20:38 -04:00
|
|
|
|
.l-main-section
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
## Revise the *HeroDetailComponent*
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
## 修改*HeroDetailComponent*
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-10 09:37:33 -07:00
|
|
|
|
Before we rewrite the `HeroDetailComponent`, let's review what it looks like now:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-14 14:04:30 +08:00
|
|
|
|
在重写`HeroDetailComponent`之前,我们先看看它现在的样子:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
+makeExample('toh-4/ts/app/hero-detail.component.ts', null, 'app/hero-detail.component.ts (current)')
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-08-17 13:31:40 -07:00
|
|
|
|
The template won't change. We'll display a hero the same way.
|
|
|
|
|
The big changes are driven by how we get the hero.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
模板不用修改,我们会用原来的方式显示英雄。导致这次大修的原因是如何获得这个英雄的数据。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
block route-params
|
|
|
|
|
:marked
|
|
|
|
|
We will no longer receive the hero in a parent component property binding.
|
|
|
|
|
The new `HeroDetailComponent` should take the `id` parameter from the `params` observable
|
|
|
|
|
in the `ActivatedRoute` service and use the `HeroService` to fetch the hero with that `id`.
|
|
|
|
|
|
|
|
|
|
我们不会再从父组件的属性绑定中取得英雄数据。
|
|
|
|
|
新的`HeroDetailComponent`应该从`ActivatedRoute`服务的可观察对象`params`中取得`id`参数,并通过`HeroService`服务获取具有这个指定`id`的英雄数据。
|
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-08-23 15:52:39 +01:00
|
|
|
|
First, add the requisite imports:
|
|
|
|
|
|
|
|
|
|
首先,添加需要的导入项目:
|
|
|
|
|
|
|
|
|
|
- var _vers = _docsFor == 'dart' ? '' : '.1'
|
|
|
|
|
+makeExcerpt('app/hero-detail.component' + _vers + '.ts', 'added-imports', '')
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
- var _ActivatedRoute = _docsFor == 'dart' ? 'RouteParams' : 'ActivatedRoute'
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-08-23 15:52:39 +01:00
|
|
|
|
Let's have the `!{_ActivatedRoute}` service and the `HeroService` injected
|
|
|
|
|
into the constructor, saving their values in private fields:
|
|
|
|
|
|
|
|
|
|
然后注入`!{_ActivatedRoute}`和`HeroService`服务到构造函数中,将他们的值保存到私有变量中:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/hero-detail.component.ts (constructor)', 'ctor')
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2016-03-15 13:24:50 +01:00
|
|
|
|
:marked
|
2016-08-09 17:38:25 +01:00
|
|
|
|
We tell the class that we want to implement the `OnInit` interface.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-12 07:57:51 +08:00
|
|
|
|
我们告诉这个类,我们要实现`OnInit`接口。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/hero-detail.component.ts', 'implement', '')(format=".")
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
block ngOnInit
|
|
|
|
|
:marked
|
|
|
|
|
Inside the `ngOnInit` lifecycle hook, we use the `params` observable to
|
|
|
|
|
extract the `id` parameter value from the `ActivateRoute` service
|
|
|
|
|
and use the `HeroService` to fetch the hero with that `id`.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
在`ngOnInit`生命周期钩子中,从`RouteParams`服务中提取`id`参数,并且使用`HeroService`来获得具有这个`id`的英雄数据。
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/hero-detail.component.ts', 'ngOnInit')
|
2016-07-15 17:30:05 +01:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
block extract-id
|
|
|
|
|
:marked
|
|
|
|
|
Notice how we extract the `id` by calling the `forEach` method
|
|
|
|
|
which will deliver our !{_array} of route parameters.
|
|
|
|
|
|
|
|
|
|
注意我们提取`id`的方法:调用`forEach`方法,它会提供一个路由参数的数组。
|
|
|
|
|
- var _str2int = _docsFor == 'dart' ? '<code>int.parse</code> static method' : 'JavaScript (+) operator'
|
2016-08-12 07:57:51 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
The hero `id` is a number. Route parameters are *always strings*.
|
2016-08-17 13:31:40 -07:00
|
|
|
|
So we convert the route parameter value to a number with the !{_str2int}.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
英雄的`id`是数字,而路由参数的值*总是字符串*。
|
2016-04-17 23:31:22 +08:00
|
|
|
|
所以我们需要通过JavaScript的(+)操作符把路由参数的值转成数字。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
### Add *HeroService.getHero*
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
### 添加*HeroService.getHero*
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
The problem with this bit of code is that `HeroService` doesn't have a `getHero` method!
|
|
|
|
|
We better fix that quickly before someone notices that we broke the app.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
这段代码的问题在于`HeroService`并没有一个叫`getHero`的方法,我们最好在别人报告应用出问题之前赶快修复它。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-29 22:09:06 +01:00
|
|
|
|
Open `HeroService` and add a `getHero` method that filters the heroes list from `getHeroes` by `id`:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-01 09:51:27 +01:00
|
|
|
|
打开`HeroService`,并添加一个`getHero`方法,用来通过`id`从`getHeros`过滤英雄列表:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/hero.service.ts', 'getHero')
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-08-17 13:31:40 -07:00
|
|
|
|
Let's return to the `HeroDetailComponent` to clean up loose ends.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
回到`HeroDetailComponent`来完成收尾工作。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
### Find our way back
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-17 23:31:22 +08:00
|
|
|
|
### 回到原路
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
|
|
|
|
We can navigate *to* the `HeroDetailComponent` in several ways.
|
2015-12-23 09:42:57 -08:00
|
|
|
|
How do we navigate somewhere else when we're done?
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
我们能用多种方式导航*到*`HeroDetailComponent`。
|
2016-04-17 23:31:22 +08:00
|
|
|
|
但当我们完工时,我们该导航到那里呢?
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
The user could click one of the two links in the `AppComponent`. Or click the browser's back button.
|
2016-08-17 13:31:40 -07:00
|
|
|
|
We'll add a third option, a `goBack` method that navigates backward one step in the browser's history stack.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
现在用户可以点击`AppComponent`中的两个链接,或点击浏览器的“后退”按钮。
|
|
|
|
|
我们来添加第三个选项:一个`goBack`方法,来根据浏览器的历史堆栈,后退一步。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/hero-detail.component.ts', 'goBack')
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
- var _CanDeactivateGuard = _docsFor == 'dart' ? '<em>routerCanDeactivate</em> hook' : '<em>CanDeactivate</em> guard'
|
|
|
|
|
- var _CanDeactivateGuardUri = _docsFor == 'dart' ? 'angular2.router/CanDeactivate-class' : 'router/index/CanDeactivate-interface'
|
2015-12-23 09:42:57 -08:00
|
|
|
|
.l-sub-section
|
2016-08-23 15:52:39 +01:00
|
|
|
|
:marked
|
|
|
|
|
Going back too far could take us out of the application.
|
|
|
|
|
That's acceptable in a demo. We'd guard against it in a real application,
|
|
|
|
|
perhaps with the [!{_CanDeactivateGuard}](../api/!{_CanDeactivateGuardUri}.html).
|
|
|
|
|
|
|
|
|
|
回退太多步儿会跑出我们的应用。
|
|
|
|
|
在Demo中,这算不上问题。但在真实的应用中,我们需要对此进行防范。
|
|
|
|
|
也许你该用[!{_CanDeactivateGuard}](../api/!{_CanDeactivateGuardUri}.html).。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
Then we wire this method with an event binding to a *Back* button that we add to the bottom of the component template.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
然后,我们通过一个事件绑定把此方法绑定到模板底部的*后退(Back)*按钮上。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/hero-detail.component.html', 'back-button', '')
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
Modifing the template to add this button spurs us to take one more incremental improvement and migrate the template to its own file
|
2016-08-23 15:52:39 +01:00
|
|
|
|
called <span ngio-ex>hero-detail.component.html</span>
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
修改模板,添加这个按钮以提醒我们还要做更多的改进,并把模板移到独立的<span ngio-ex>hero-detail.component.html</span>文件中去。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExample('app/hero-detail.component.html')
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
We update the component metadata with a `templateUrl` pointing to the template file that we just created.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
然后更新组件的元数据,用一个`templateUrl`属性指向我们刚刚创建的模板文件。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/hero-detail.component.ts', 'templateUrl')
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
Here's the (nearly) finished `HeroDetailComponent`:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-14 09:26:54 +08:00
|
|
|
|
下面是(几乎)完成的`HeroDetailComponent`:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
+makeExample('toh-5/ts/app/hero-detail.component.ts', 'v2', 'app/hero-detail.component.ts (latest)')(format=".")
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.l-main-section
|
|
|
|
|
:marked
|
|
|
|
|
## Select a *Dashboard* Hero
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
## 选择一个*仪表盘*中的英雄
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-10 09:37:33 -07:00
|
|
|
|
When a user selects a hero in the dashboard, the app should navigate to the `HeroDetailComponent` to view and edit the selected hero.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
当用户从仪表盘中选择了一位英雄时,本应用要导航到`HeroDetailComponent`以查看和编辑所选的英雄。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
In the dashboard template we bound each hero's click event to the `gotoDetail` method, passing along the selected `hero` entity.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
在仪表盘模板中,我们把每个英雄的click事件都绑定成`gotoDetail`方法,并且传入选中的这个`hero`实体对象。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
+makeExample('toh-5/ts/app/dashboard.component.html','click', 'app/dashboard.component.html (click绑定)')(format=".")
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-07-01 08:26:45 +08:00
|
|
|
|
We stubbed the `gotoDetail` method when we rewrote the `DashboardComponent`.
|
|
|
|
|
Now we give it a real implementation.
|
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
当初我们重写`DashboardComponent`的时候,`gotoDetail`还是一个“桩方法”。
|
|
|
|
|
现在,我们给它一个真正的实现。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/dashboard.component.ts','gotoDetail')
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
The `gotoDetail` method navigates in two steps:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
`gotoDetail`方法分两步完成导航:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
1. Set a route *link parameters !{_array}*
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
1. 生成路由的 *链接参数数组*
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
1. Pass the !{_array} to the router's navigate method
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
1. 把这个数组传给路由器的navigate方法。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
For navigation, we wrote router links <span if-docs="dart">as *link
|
|
|
|
|
parameters !{_array}s*</span> in the [`AppComponent`
|
|
|
|
|
template](#router-links). Those link<span if-docs="dart"> parameters
|
|
|
|
|
!{_array}</span>s had only one element, the !{_pathVsName} of the
|
|
|
|
|
destination route.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
我们为导航在[`AppComponent`的模板](#router-links)中生成导航链接<span if-docs="dart">as *link
|
|
|
|
|
parameters !{_array}s*</span>。这些链接<span if-docs="dart"> parameters
|
|
|
|
|
!{_array}</span>只有一个元素:目标路由的路径。
|
|
|
|
|
|
|
|
|
|
This link parameters !{_array} has two elements, the ***!{_pathVsName}*** of
|
|
|
|
|
the destination route and a ***route parameter*** <span if-docs="dart">with
|
|
|
|
|
an `id` field</span> set to the value of the selected hero's `id`.
|
|
|
|
|
|
|
|
|
|
这个链接参数数组有两个元素:目标路由的***路径(path)***和一个***路由参数对象***,其中包括一个`id`字段,它的取值是所选英雄的`id`。
|
|
|
|
|
|
|
|
|
|
The two !{_array} items align with the ***!{_pathVsName}*** and ***:id***
|
|
|
|
|
token in the parameterized hero detail route definition we added to
|
|
|
|
|
`!{_appRoutingTsVsAppComp}` earlier in the chapter:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
这两个数组项目与我们之前在`!{_appRoutingTsVsAppComp}`中添加的***path***和***:id***为代号被参数化的英雄详情路由的配置对象对应。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
- var _file = _docsFor == 'dart' ? 'app/app.component.ts' : 'app/app.routing.ts'
|
|
|
|
|
+makeExcerpt(_file + ' (hero detail)', 'hero-detail')
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-06-19 00:20:38 -04:00
|
|
|
|
The `DashboardComponent` doesn't have the router yet. We obtain it in the usual way:
|
2016-06-10 09:37:33 -07:00
|
|
|
|
import the `router` reference and inject it in the constructor (along with the `HeroService`):
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
`DashboardComponent`还没有路由器。我们使用常规的方式为它加上路由:
|
2016-06-14 09:26:54 +08:00
|
|
|
|
`import` `router`对象的引用,并且把它注入到构造函数中(就像`HeroService`那样):
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/dashboard.component.ts ()','import-router', '')
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/dashboard.component.ts', 'ctor', '')
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
Refresh the browser and select a hero from the dashboard; the app should navigate directly to that hero’s details.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
刷新浏览器,并从仪表盘中选择一位英雄,应用就会直接导航到英雄的详情。
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
|
|
|
|
.l-main-section
|
|
|
|
|
:marked
|
|
|
|
|
## Select a Hero in the *HeroesComponent*
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
## 在*HeroesComponent*中选择一位英雄
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
We'll do something similar in the `HeroesComponent`.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
我们现在要做的事和`HeroesComponent`中很像。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
That component's current template exhibits a "master/detail" style with the list of heroes
|
|
|
|
|
at the top and details of the selected hero below.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
那个组件的当前模板展示了一个主从风格的界面:上方是英雄列表,底下是所选英雄的详情。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
+makeExample('toh-4/ts/app/app.component.ts','template', 'app/heroes.component.ts (当前的模板)')(format=".")
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
Delete the last line of the template with the `<my-hero-detail>` tags.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
删除模板最后带有`<my-hero-detail>`标签的那一行。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
|
|
|
|
We'll no longer show the full `HeroDetailComponent` here.
|
2015-12-23 09:42:57 -08:00
|
|
|
|
We're going to display the hero detail on its own page and route to it as we did in the dashboard.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
这里我们不再展示完整的`HeroDetailComponent`了。
|
2016-05-12 22:56:12 +08:00
|
|
|
|
我们要在它自己的页面中显示英雄详情,并像我们在仪表盘中所做的那样路由到它。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
|
|
|
|
But we'll throw in a small twist for variety.
|
2015-12-23 09:42:57 -08:00
|
|
|
|
When the user selects a hero from the list, we *won't* go to the detail page.
|
|
|
|
|
We'll show a *mini-detail* on *this* page instead and make the user click a button to navigate to the *full detail *page.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
但是,我们要做一点小小的改动。
|
2016-05-12 22:56:12 +08:00
|
|
|
|
当用户从这个列表中选择一个英雄时,我们*不会*再跳转到详情页。
|
|
|
|
|
而是在本页中显示一个*Mini版英雄详情*,并等用户点击一个按钮时,才导航到*完整版英雄详情*页。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
### Add the *mini-detail*
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
### 添加*Mini版英雄详情*
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
Add the following HTML fragment at the bottom of the template where the `<my-hero-detail>` used to be:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
在模板底部原来放`<my-hero-detail>`的地方添加下列HTML片段:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/heroes.component.html', 'mini-detail', '')
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
After clicking a hero, the user should see something like this below the hero list:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
点击一个英雄,用户将会在英雄列表的下方看到这些:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
figure.image-display
|
2016-04-18 22:59:24 +08:00
|
|
|
|
img(src='/resources/images/devguide/toh/mini-hero-detail.png' alt="Mini版英雄详情" height="70")
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-08-17 13:31:40 -07:00
|
|
|
|
### Format with the *uppercase* pipe
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
### 使用*UpperCasePipe*格式化
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
Notice that the hero's name is displayed in CAPITAL LETTERS. That's the effect of the `uppercase` pipe
|
2016-06-10 09:37:33 -07:00
|
|
|
|
that we slipped into the interpolation binding. Look for it right after the pipe operator ( | ).
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
注意,英雄的名字全被显示成大写字母。那是 `uppercase`管道的效果,借助它,我们能插手“插值表达式绑定”的过程。去管道操作符 ( | ) 后面找它。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/heroes.component.html', 'pipe', '')
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
Pipes are a good way to format strings, currency amounts, dates and other display data.
|
|
|
|
|
Angular ships with several pipes and we can write our own.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
管道擅长做下列工作:格式化字符串、金额、日期和其它显示数据。
|
2016-05-12 22:56:12 +08:00
|
|
|
|
Angular自带了好几个管道,而且我们还可以写自己的管道。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
|
|
|
|
Learn about pipes in the [Pipes](../guide/pipes.html) chapter.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
要学习关于管道的更多知识,参见[管道](../guide/pipes.html)一章。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
### Move content out of the component file
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
### 把内容移出组件文件
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
We are not done. We still have to update the component class to support navigation to the
|
|
|
|
|
`HeroDetailComponent` when the user clicks the *View Details* button.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
这还没完。当用户点击*查看详情*按钮时,要让他能导航到`HeroDetailComponent`,我们仍然不得不修改组件类。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
|
|
|
|
This component file is really big. Most of it is either template or CSS styles.
|
2015-12-23 09:42:57 -08:00
|
|
|
|
It's difficult to find the component logic amidst the noise of HTML and CSS.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
这个组件文件太大了。它大部分都是模板或css样式。
|
2016-05-12 22:56:12 +08:00
|
|
|
|
要想在HTML和CSS的噪音中看清组件的工作逻辑太难了。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
Let's migrate the template and the styles to their own files before we make any more changes:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
在做更多修改之前,我们先把模板和样式移到它们自己的文件中去:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
1. *Cut-and-paste* the template contents into a new <span ngio-ex>heroes.component.html</span> file.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
1. 把模板内容*剪切并粘贴*到新的<span ngio-ex>heroes.component.html</span>文件。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
1. *Cut-and-paste* the styles contents into a new <span ngio-ex>heroes.component.css</span> file.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
1. 把样式内容*剪切并粘贴*到新的<span ngio-ex>heroes.component.css</span>文件。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-20 20:07:39 +08:00
|
|
|
|
1. *Set* the component metadata's `templateUrl` and `styleUrls` properties to refer to both files.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
1. *设置*组件元数据的`templateUrl`和`styleUrls`属性,来分别引用这两个文件。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
|
|
|
|
The `styleUrls` property is !{_an} !{_array} of style file names (with paths).
|
|
|
|
|
We could list multiple style files from different locations if we needed them.
|
|
|
|
|
<span if-docs="ts">As with `templateUrl`, we must specify the path _all the way
|
|
|
|
|
back to the application root_.</span>
|
|
|
|
|
|
|
|
|
|
`styleUrls`属性是一个由样式文件的文件名(包括路径)组成的数组。
|
|
|
|
|
我们还可以列出来自多个不同位置的样式文件。
|
|
|
|
|
和`templateUrl`一样,必须指定_相对于此应用根目录_的路径。
|
|
|
|
|
|
|
|
|
|
block heroes-component-cleanup
|
|
|
|
|
//- Only relevant for Dart.
|
|
|
|
|
|
|
|
|
|
+makeExcerpt('app/heroes.component.ts (revised metadata)', 'metadata')
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
Now we can see what's going on as we update the component class along the same lines as the dashboard:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
现在,我们一下就明白该怎么像仪表盘中那样更新组件类了:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-20 20:07:39 +08:00
|
|
|
|
1. Import the `router`
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
1. 导入`router`
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-20 20:07:39 +08:00
|
|
|
|
1. Inject the `router` in the constructor (along with the `HeroService`)
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-14 09:26:54 +08:00
|
|
|
|
1. 把`router`注入到构造函数中(就像`HeroService`那样)
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-24 14:25:47 +01:00
|
|
|
|
1. Implement the `gotoDetail` method by calling the `router.navigate` method with a two-part hero-detail link parameters !{_array}.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
1. 实现`gotoDetail`方法:以`HeroDetail`和*链接参数数组*为参数调用`router.navigate`方法。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
Here's the revised component class:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
下面是修改过的组件类:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/heroes.component.ts', 'class')
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-06-19 00:20:38 -04:00
|
|
|
|
Refresh the browser and start clicking.
|
2015-12-23 09:42:57 -08:00
|
|
|
|
We can navigate around the app, from the dashboard to hero details and back,
|
2016-06-19 00:20:38 -04:00
|
|
|
|
for heroes list to the mini-detail to the hero details and back to the heroes again.
|
2015-12-23 09:42:57 -08:00
|
|
|
|
We can jump back and forth between the dashboard and the heroes.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
刷新浏览器,并开始点击。
|
|
|
|
|
我们能在应用中导航:从仪表盘到英雄详情再回来,从英雄列表到Mini版英雄详情到英雄详情,再回到英雄列表。
|
2016-04-18 22:59:24 +08:00
|
|
|
|
我们可以在仪表盘和英雄列表之间跳来跳去。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
We've met all of the navigational requirements that propelled this chapter.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
我们已经满足了在本章开头儿设定的所有导航需求。
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
|
|
|
|
.l-main-section
|
|
|
|
|
:marked
|
|
|
|
|
## Styling the App
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
## 美化本应用
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
The app is functional but pretty ugly.
|
|
|
|
|
Our creative designer team provided some CSS files to make it look better.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
应用在功能上已经正常了,但还太丑。
|
|
|
|
|
我们富有创意的设计师团队提供了一些CSS文件,能让它变得好看一些。
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
|
|
|
|
### A Dashboard with Style
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
### 具有样式的仪表盘
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
|
|
|
|
The designers think we should display the dashboard heroes in a row of rectangles.
|
2015-12-23 09:42:57 -08:00
|
|
|
|
They've given us ~60 lines of CSS for this purpose including some simple media queries for responsive design.
|
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
设计师认为我们应该把仪表盘的英雄们显示在一排方块中。
|
2016-05-12 22:56:12 +08:00
|
|
|
|
他们给了我们大约60行CSS来实现它,包括一些简单的媒体查询语句来实现响应式设计。
|
2016-04-18 22:59:24 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
If we paste these ~60 lines into the component `styles` metadata,
|
2016-07-01 08:26:45 +08:00
|
|
|
|
they'll completely obscure the component logic.
|
|
|
|
|
Let's not do that. It's easier to edit CSS in a separate `*.css` file anyway.
|
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
如果我们把这60来行CSS粘贴到组件元数据的`styles`中,它们会完全淹没组件的工作逻辑。
|
2016-05-12 22:56:12 +08:00
|
|
|
|
不能这么做。在一个独立的`*.css`文件中编辑CSS当然会更简单。
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
Add a <span ngio-ex>dashboard.component.css</span> file to the `!{_appDir}` folder and reference
|
|
|
|
|
that file in the component metadata's `styleUrls` !{_array} property like this:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
把<span ngio-ex>dashboard.component.css</span>文件添加到`!{_appDir}`目录下,并在组件元数据的`styleUrls`数组属性中引用它。就像这样:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/dashboard.component.ts (styleUrls)', 'css')
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
### Stylish Hero Details
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
### 美化英雄详情
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
The designers also gave us CSS styles specifically for the `HeroDetailComponent`.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
设计师还给了我们`HeroDetailComponent`特有的CSS风格。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
Add a <span ngio-ex>hero-detail.component.css</span> to the `!{_appDir}`
|
|
|
|
|
folder and refer to that file inside
|
|
|
|
|
the `styleUrls` !{_array} as we did for `DashboardComponent`.
|
|
|
|
|
Let's also remove the `hero` property `@Input` !{_decorator}
|
|
|
|
|
<span if-docs="ts">and its import</span>
|
|
|
|
|
while we are at it.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
在`!{_appDir}`目录下添加<span ngio-ex>hero-detail.component.css</span>文件,
|
|
|
|
|
并且在`styleUrls`数组中引用它 —— 就像当初在`DashboardComponent`中做过的那样。
|
|
|
|
|
同时删除`hero``@Input`装饰器属性<span if-docs="ts">和它的导入语句</span>。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
|
|
|
|
Here's the content for the aforementioned component CSS files.
|
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
上述组件的CSS文件内容如下:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
block css-files
|
|
|
|
|
+makeTabs(
|
|
|
|
|
`toh-5/ts/app/hero-detail.component.css,
|
|
|
|
|
toh-5/ts/app/dashboard.component.css`,
|
|
|
|
|
null,
|
|
|
|
|
`app/hero-detail.component.css,
|
|
|
|
|
app/dashboard.component.css`)
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
### Style the Navigation Links
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
### 美化导航链接
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
The designers gave us CSS to make the navigation links in our `AppComponent` look more like selectable buttons.
|
|
|
|
|
We cooperated by surrounding those links in `<nav>` tags.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
设计师还给了我们一些CSS,用于让`AppComponent`中的导航链接看起来更像可被选择的按钮。
|
2016-05-12 22:56:12 +08:00
|
|
|
|
要让它们协同工作,我们得把那些链接包含在`<nav>`标签中。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
Add a <span ngio-ex>app.component.css</span> file to the `!{_appDir}` folder with the following content.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
在`!{_appDir}`目录下添加一个<span ngio-ex>app.component.css</span>文件,内容如下:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/app.component.css (navigation styles)', '')
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
.l-sub-section
|
2016-08-17 13:31:40 -07:00
|
|
|
|
block router-link-active
|
|
|
|
|
:marked
|
|
|
|
|
**The *routerLinkActive* directive**
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
***routerLinkActive*指令**
|
|
|
|
|
|
|
|
|
|
The Angular Router provides a `routerLinkActive` directive we can use to
|
|
|
|
|
add a class to the HTML navigation element whose route matches the active route.
|
|
|
|
|
All we have to do is define the style for it. Sweet!
|
|
|
|
|
|
|
|
|
|
Angular路由器提供了`routerLinkActive`指令,我们可以用它来为匹配了活动路由的HTML导航元素自动添加一个CSS类。
|
|
|
|
|
我们唯一要做的就是为它定义样式。真好!
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/app.component.ts (active router links)', 'template')
|
2016-07-01 08:24:23 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-07-01 08:26:45 +08:00
|
|
|
|
Set the `AppComponent`’s `styleUrls` property to this CSS file.
|
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
设置`AppComponent`的`styleUrls`属性,指向这个CSS文件。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('app/app.component.ts','styleUrls')
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
|
|
|
|
### Global application styles
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
### 应用的全局样式
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
|
|
|
|
When we add styles to a component, we're keeping everything a component needs
|
2015-12-23 09:42:57 -08:00
|
|
|
|
— HTML, the CSS, the code — together in one convenient place.
|
|
|
|
|
It's pretty easy to package it all up and re-use the component somewhere else.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
当我们把样式添加到组件中时,我们假定组件所需的一切 —— HTML、CSS、程序代码 —— 都在紧邻的地方。
|
2016-05-12 22:56:12 +08:00
|
|
|
|
这样,无论是把它们打包在一起还是在别的组件中复用它都会很容易。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
We can also create styles at the *application level* outside of any component.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
我们也可以在所有组件之外创建*应用级*样式。
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
2016-06-10 09:37:33 -07:00
|
|
|
|
Our designers provided some basic styles to apply to elements across the entire app.
|
2016-06-19 00:20:38 -04:00
|
|
|
|
These correspond to the full set of master styles that we
|
|
|
|
|
introduced earlier (see
|
2016-06-10 09:37:33 -07:00
|
|
|
|
[QuickStart, "Add some style"](../quickstart.html#!#add-some-style)).
|
2016-08-17 13:31:40 -07:00
|
|
|
|
Here is an excerpt:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
我们的设计师提供了一组基础样式,这些样式应用到的元素横跨整个应用。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
这些和我们前面引入的主样式全集是一样的(参见
|
2016-06-14 14:04:30 +08:00
|
|
|
|
[快速起步, "添加一些样式"](../quickstart.html#!#add-some-style))。
|
|
|
|
|
下面是摘录:
|
2016-06-10 09:37:33 -07:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('styles.css (excerpt)', 'toh')
|
2016-06-10 09:37:33 -07:00
|
|
|
|
|
|
|
|
|
- var styles_css = 'https://raw.githubusercontent.com/angular/angular.io/master/public/docs/_examples/styles.css'
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
|
|
|
|
:marked
|
2016-08-17 13:31:40 -07:00
|
|
|
|
Create the file <span ngio-ex>styles.css</span>, if it doesn't exist already.
|
2016-06-10 09:37:33 -07:00
|
|
|
|
Ensure that it contains the [master styles given here](!{styles_css}).
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-14 14:04:30 +08:00
|
|
|
|
如果在根目录下没有一个名叫`styles.css`的文件,就添加它。
|
|
|
|
|
确保它包含[这里给出的主样式](!{styles_css})。
|
2016-06-10 09:37:33 -07:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
If necessary, also edit <span ngio-ex>index.html</span> to refer to this stylesheet.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
如有必要,也可以编辑<span ngio-ex>index.html</span>来引用这个样式表。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
+makeExcerpt('index.html (link ref)', 'css')
|
2016-06-15 23:21:05 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
:marked
|
2016-08-17 13:31:40 -07:00
|
|
|
|
Look at the app now. Our dashboard, heroes, and navigation links are styling!
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
看看现在的应用!我们的仪表盘、英雄列表和导航链接都漂亮多了!
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
figure.image-display
|
2016-04-18 22:59:24 +08:00
|
|
|
|
img(src='/resources/images/devguide/toh/dashboard-top-heroes.png' alt="查看导航栏")
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
|
|
|
|
.l-main-section
|
|
|
|
|
:marked
|
|
|
|
|
## Application structure and code
|
2016-07-07 08:15:51 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
## 应用结构和代码
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-07-03 17:11:17 -07:00
|
|
|
|
Review the sample source code in the <live-example></live-example> for this chapter.
|
2015-12-23 09:42:57 -08:00
|
|
|
|
Verify that we have the following structure:
|
2016-07-07 08:15:51 +08:00
|
|
|
|
|
|
|
|
|
在<live-example></live-example>中回顾本章的范例代码。
|
|
|
|
|
验证我们是否已经得到了如下结构:
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
2016-08-17 13:31:40 -07:00
|
|
|
|
block file-tree-end
|
|
|
|
|
.filetree
|
|
|
|
|
.file angular2-tour-of-heroes
|
2015-12-23 09:42:57 -08:00
|
|
|
|
.children
|
2016-08-17 13:31:40 -07:00
|
|
|
|
.file app
|
|
|
|
|
.children
|
|
|
|
|
.file app.component.css
|
|
|
|
|
.file app.component.ts
|
|
|
|
|
.file app.module.ts
|
|
|
|
|
.file app.routing.ts
|
|
|
|
|
.file dashboard.component.css
|
|
|
|
|
.file dashboard.component.html
|
|
|
|
|
.file dashboard.component.ts
|
|
|
|
|
.file hero.service.ts
|
|
|
|
|
.file hero.ts
|
|
|
|
|
.file hero-detail.component.css
|
|
|
|
|
.file hero-detail.component.html
|
|
|
|
|
.file hero-detail.component.ts
|
|
|
|
|
.file heroes.component.css
|
|
|
|
|
.file heroes.component.html
|
|
|
|
|
.file heroes.component.ts
|
|
|
|
|
.file main.ts
|
|
|
|
|
.file mock-heroes.ts
|
|
|
|
|
.file node_modules ...
|
|
|
|
|
.file typings ...
|
|
|
|
|
.file index.html
|
|
|
|
|
.file package.json
|
|
|
|
|
.file styles.css
|
|
|
|
|
.file systemjs.config.js
|
|
|
|
|
.file tsconfig.json
|
|
|
|
|
.file typings.json
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
|
|
|
|
.l-main-section
|
|
|
|
|
:marked
|
|
|
|
|
## Recap
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
## 总结
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
|
|
|
|
### The Road Behind
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
### 走过的路
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
We travelled a great distance in this chapter
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
在本章中,我们往前走了很远:
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-20 20:07:39 +08:00
|
|
|
|
- We added the Angular *Component Router* to navigate among different components.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
- 我们添加了Angular*组件路由器*在各个不同组件之间导航。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-10 09:37:33 -07:00
|
|
|
|
- We learned how to create router links to represent navigation menu items.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
- 我们学会了如何创建路由链接来表示导航栏的菜单项。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
- We used router link parameters to navigate to the details of user selected hero.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-08-23 15:52:39 +01:00
|
|
|
|
- 我们使用路由链接参数来导航到用户所选的英雄详情。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-10 09:37:33 -07:00
|
|
|
|
- We shared the `HeroService` among multiple components.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
- 我们在多个组件之间共享了`HeroService`服务。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-20 20:07:39 +08:00
|
|
|
|
- We moved HTML and CSS out of the component file and into their own files.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-05-12 22:56:12 +08:00
|
|
|
|
- 我们把HTML和CSS从组件中移出来,放到了它们自己的文件中。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-10 09:37:33 -07:00
|
|
|
|
- We added the `uppercase` pipe to format data.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
- 我们添加了一个`uppercase`管道,来格式化数据。
|
2015-12-23 09:42:57 -08:00
|
|
|
|
|
|
|
|
|
### The Road Ahead
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
### 前方的路
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2015-12-23 09:42:57 -08:00
|
|
|
|
We have much of the foundation we need to build an application.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
We're still missing a key piece: remote data access.
|
|
|
|
|
|
2016-04-18 22:59:24 +08:00
|
|
|
|
我们有了很多用于构建应用的基石。
|
2016-05-12 22:56:12 +08:00
|
|
|
|
但我们仍然缺少很关键的一块儿:远程数据存取。
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
|
|
|
|
In the next chapter,
|
2016-04-17 23:31:22 +08:00
|
|
|
|
we’ll replace our mock data with data retrieved from a server using http.
|
2016-07-01 08:26:45 +08:00
|
|
|
|
|
2016-06-14 14:04:30 +08:00
|
|
|
|
在下一章,我们将从硬编码模拟数据改为使用http服务从服务器获取数据。
|
2016-05-03 22:33:20 +08:00
|
|
|
|
|