block includes
include ../_util-fns
:marked
There are new requirements for the Tour of Heroes app:
我们收到了《英雄指南》的一些新需求:
* Add a *Dashboard* view.
添加一个*仪表盘*视图。
* Add the ability to navigate between the *Heroes* and *Dashboard* views.
在*英雄列表*和*仪表盘*视图之间导航。
* When users click a hero name in either view, navigate to a detail view of the selected hero.
无论在哪个视图中点击一个英雄,都会导航到该英雄的详情页。
* When users click a *deep link* in an email, open the detail view for a particular hero.
在邮件中点击一个*深链接*,会直接打开一个特定英雄的详情视图。
When you’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
To satisfy these requirements, you'll add Angular’s router to the app.
我们将把 Angular *路由器*加入应用中,以满足这些需求。
(译注:硬件领域中的路由器是用来帮你找到另一台网络设备的,而这里的路由器用于帮你找到一个组件)
.l-sub-section
:marked
For more information about the router, read the [Routing and Navigation](../guide/router.html) page.
更多信息,见[路由和导航](../guide/router.html)。
:marked
When you're done with this page, the app should look like this app.component.ts
file to heroes.component.ts
.
把app.component.ts
文件改名为heroes.component.ts
。
* Rename the `AppComponent` class as `HeroesComponent` (rename locally, _only_ in this file).
把`AppComponent`类改名为`HeroesComponent`(注意,*只*在这一个文件中改名)。
* Rename the selector `my-app` as `my-heroes`.
把`my-app`选择器改名为`my-heroes`。
+makeExample('toh-5/ts/src/app/heroes.component.ts', 'renaming', 'src/app/heroes.component.ts (showing renamings only)')
:marked
### Create *AppComponent*
### 创建 *AppComponent*
The new `AppComponent` is the application shell.
It will have some navigation links at the top and a display area below.
新的`AppComponent`将成为应用的“壳”。
它将在顶部放一些导航链接,并且把我们要导航到的页面放在下面的显示区中。
Perform these steps:
执行下列步骤:
* add the supporting `import` statements.
添加支持性的`import`语句。
* Create the file src/app/app.component.ts
.
创建一个名叫src/app/app.component.ts的新文件。
* Define an exported `AppComponent` class.
定义一个导出的 `AppComponent`类。
* Add an `@Component` decorator above the class with a `my-app` selector.
在类的上方添加`@Component`元数据装饰器,装饰器带有`my-app`选择器。
* Move the following from `HeroesComponent` to `AppComponent`:
将下面的项目从`HeroesComponent`移到`AppComponent`:
* `title` class property.
`title`类属性
* `@Component` template `
providers
中移除HeroService
:marked
Go back to the `HeroesComponent` and **remove the `HeroService`** from its `providers` array.
We are *promoting* this service from the `HeroesComponent` to the root `NgModule`.
We ***do not want two copies*** of this service at two different levels of our app.
回到`HeroesComponent`,并从`providers`数组中**移除`HeroService`**。
把它从`HeroesComponent`*提升*到根`NgModule`中。
我们不希望在应用的两个不同层次上存在它的***两个副本***。
:marked
The app still runs and displays heroes.
应用仍然在运行,并显示着英雄列表。
:marked
## Add routing
## 添加路由
Instead of displaying automatically, heroes should display after users click a button.
In other words, users should be able to navigate to the list of heroes.
我们希望在用户点击按钮之后才显示英雄列表,而不是自动显示。
换句话说,我们希望用户能“导航”到英雄列表。
Use the Angular router to enable navigation.
我们要使用 Angular *路由器*进行导航。
: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`). You'll configure the routes first.
Angular 路由器是一个可选的外部 Angular NgModule,名叫`RouterModule`。
路由器包含了多种服务(`RouterModule`)、多种指令(`RouterOutlet、RouterLink、RouterLinkActive`)、
和一套配置(`Routes`)。我们将先配置路由。
:marked
### *<base href>*
Open `index.html` and ensure there is a `