devguide(router): child route rename - "CrisisCenter" -> "CrisisList"

This commit is contained in:
Ward Bell 2016-02-16 09:18:46 -08:00
parent 8c291ab760
commit d4914eed26
6 changed files with 33 additions and 31 deletions

View File

@ -27,7 +27,7 @@ import {HeroService} from './heroes/hero.service';
*/ */
/* Crisis Center link when CC lacks a default /* Crisis Center link when CC lacks a default
// #docregion cc-anchor-no-default // #docregion cc-anchor-no-default
<a [routerLink]="['CrisisCenter', 'CrisisCenter']">Crisis Center</a> <a [routerLink]="['CrisisCenter', 'CrisisList']">Crisis Center</a>
// #enddocregion cc-anchor-no-default // #enddocregion cc-anchor-no-default
*/ */
/* Crisis Center Detail link /* Crisis Center Detail link
@ -39,7 +39,7 @@ import {HeroService} from './heroes/hero.service';
template: ` template: `
<h1 class="title">Component Router</h1> <h1 class="title">Component Router</h1>
<nav> <nav>
<a [routerLink]="['CrisisCenter', 'CrisisCenter']">Crisis Center</a> <a [routerLink]="['CrisisCenter', 'CrisisList']">Crisis Center</a>
<a [routerLink]="['CrisisCenter', 'CrisisDetail', {id:1}]">Princess Crisis</a> <a [routerLink]="['CrisisCenter', 'CrisisDetail', {id:1}]">Princess Crisis</a>
<a [routerLink]="['CrisisCenter', 'CrisisDetail', {id:2}]">Dragon Crisis</a> <a [routerLink]="['CrisisCenter', 'CrisisDetail', {id:2}]">Dragon Crisis</a>
</nav> </nav>

View File

@ -19,7 +19,7 @@ import {CrisisService} from './crisis.service';
// #docregion route-config // #docregion route-config
@RouteConfig([ @RouteConfig([
// #docregion default-route // #docregion default-route
{path:'/', name: 'CrisisCenter', component: CrisisListComponent, useAsDefault: true}, {path:'/', name: 'CrisisList', component: CrisisListComponent, useAsDefault: true},
// #enddocregion default-route // #enddocregion default-route
{path:'/:id', name: 'CrisisDetail', component: CrisisDetailComponent} {path:'/:id', name: 'CrisisDetail', component: CrisisDetailComponent}
]) ])

View File

@ -15,7 +15,7 @@ import {CrisisService} from './crisis.service';
providers: [CrisisService] providers: [CrisisService]
}) })
@RouteConfig([ @RouteConfig([
{path:'/', name: 'CrisisCenter', component: CrisisListComponent, useAsDefault: true}, {path:'/', name: 'CrisisList', component: CrisisListComponent, useAsDefault: true},
{path:'/:id', name: 'CrisisDetail', component: CrisisDetailComponent} {path:'/:id', name: 'CrisisDetail', component: CrisisDetailComponent}
]) ])
export class CrisisCenterComponent { } export class CrisisCenterComponent { }

View File

@ -82,8 +82,8 @@ export class CrisisDetailComponent implements OnInit, CanDeactivate {
// #docregion gotoCrises // #docregion gotoCrises
gotoCrises() { gotoCrises() {
// Like <a [routerLink]="['CrisisCenter']">Crisis Center</a // Like <a [routerLink]="['CrisisList']">Crisis Center</a
this._router.navigate(['CrisisCenter']); this._router.navigate(['CrisisList']);
} }
// #enddocregion gotoCrises // #enddocregion gotoCrises
// #docregion routerCanDeactivate, cancel-save // #docregion routerCanDeactivate, cancel-save

View File

@ -75,7 +75,7 @@ export class CrisisDetailComponent implements OnInit, CanDeactivate {
// so that the CrisisListComponent can select that hero. // so that the CrisisListComponent can select that hero.
// Add a totally useless `foo` parameter for kicks. // Add a totally useless `foo` parameter for kicks.
// #docregion gotoCrises-navigate // #docregion gotoCrises-navigate
this._router.navigate(['CrisisCenter', {id: crisisId, foo: 'foo'} ]); this._router.navigate(['CrisisList', {id: crisisId, foo: 'foo'} ]);
// #enddocregion gotoCrises-navigate // #enddocregion gotoCrises-navigate
} }
// #enddocregion gotoCrises // #enddocregion gotoCrises

View File

@ -412,7 +412,7 @@ figure.image-display
1. *When the application requests navigation to a route **named** `CrisisCenter`, compose a browser URL 1. *When the application requests navigation to a route **named** `CrisisCenter`, compose a browser URL
with the path segment `/crisis-center`, update the browser's address location and history, create or retrieve an instance of with the path segment `/crisis-center`, update the browser's address location and history, create or retrieve an instance of
the `CrisisCenterComponent`, and display that component's view.* the `CrisisListComponent`, and display that component's list view.*
### "Getting Started" wrap-up ### "Getting Started" wrap-up
@ -808,15 +808,17 @@ code-example(format="." language="bash").
that we did earlier. that we did earlier.
+makeExample('router/ts/app/crisis-center/crisis-center.component.1.ts', 'route-config', 'app/crisis-center/crisis-center.component.ts (routes only)' )(format=".") +makeExample('router/ts/app/crisis-center/crisis-center.component.1.ts', 'route-config', 'app/crisis-center/crisis-center.component.ts (routes only)' )(format=".")
:marked :marked
There is an *important difference in the paths*. They both begin at `/`. The two routes terminate in the two *Crisis Center* child components, `CrisisListComponent` and `CrisisDetailComponent`.
Normally such paths would refer to the root of the application.
There is an *important difference* in the treatment of the root `AppComponent` paths and these paths.
Normally paths that begin with `/` refer to the root of the application.
Here they refer to the **root of the child component!**. Here they refer to the **root of the child component!**.
The Component Router composes the final route by concatenating route paths beginning with the ancestor paths to this child router. The Component Router composes the final route by concatenating route paths beginning with the ancestor paths to this child router.
In our example, there is one ancestor path: "crisis-center". In our example, there is one ancestor path: "crisis-center".
The final route to the `HeroDetailComponent` displaying hero 11 would be something like: The final route to the `CrisisDetailComponent` displaying the crisis whose `id` is 2 would be something like:
code-example(format=""). code-example(format="").
localhost:3000//crisis-center/11 localhost:3000/crisis-center/2
:marked :marked
We cannot know this simply by looking at the `CrisisCenterComponent` alone. We cannot know this simply by looking at the `CrisisCenterComponent` alone.
We can't tell that it is a *child* routing component. We can't tell that it is a *child* routing component.
@ -858,19 +860,21 @@ code-example(format="").
We've set the top level default route to go to the `CrisisCenterComponent`. We've set the top level default route to go to the `CrisisCenterComponent`.
The final route will be a combination of `/crisis-center/` The final route will be a combination of `/crisis-center/`
and one of the child `CrisisCenterComponent` router's *three* routes. Which one? and one of the child `CrisisCenterComponent` router's two routes. Which one?
It could be any of the three. In the absence of additional information, the router can't decide and must throw an error. It could be either of them. In the absence of additional information, the router can't decide and must throw an error.
We've tried the sample application and it didn't fail. We must have done something right. We've tried the sample application and it didn't fail. We must have done something right.
Scroll to the end of the child `CrisisCenterComponent`s first route. Look at the end of the child `CrisisCenterComponent`s first route.
+makeExample('router/ts/app/crisis-center/crisis-center.component.1.ts', 'default-route', 'app/crisis-center/crisis-center.component.ts (default route)')(format=".") +makeExample('router/ts/app/crisis-center/crisis-center.component.1.ts', 'default-route', 'app/crisis-center/crisis-center.component.ts (default route)')(format=".")
:marked :marked
There is `useAsDefault: true` again. That tells the router to compose the final URL using the path from the default child route. We see `useAsDefault: true` once again.
Concatenate the base URL with `/crisis-center/` and `/`, remove extraneous slashes, and we get: That tells the router to compose the final URL using the path from the default *child* route.
Concatenate the base URL with the parent path, `/crisis-center/`, and the child path, `/`.
Remove superfluous slashes. We get:
code-example(format=""). code-example(format="").
localhost:3000//crisis-center/ localhost:3000/crisis-center/
.l-main-section .l-main-section
:marked :marked
@ -1292,25 +1296,23 @@ code-example(format="." language="bash").
These two examples cover our needs for an app with one level routing. These two examples cover our needs for an app with one level routing.
The moment we add a child router, such as the *Crisis Center*, we create new link array possibilities. The moment we add a child router, such as the *Crisis Center*, we create new link array possibilities.
We specify a default child route for *Crisis Center* so this simple `RouterLink` is fine. Recall that we specified a default child route for *Crisis Center* so this simple `RouterLink` is fine.
+makeExample('router/ts/app/app.component.3.ts', 'cc-anchor-w-default')(format=".") +makeExample('router/ts/app/app.component.3.ts', 'cc-anchor-w-default')(format=".")
:marked :marked
If we hadn't specified a default route, our single item array would fail *If we had not specified a default route*, our single item array would fail
because we didn't tell the router which child route to use. because we didn't tell the router which child route to use.
+makeExample('router/ts/app/app.component.3.ts', 'cc-anchor-fail')(format=".") +makeExample('router/ts/app/app.component.3.ts', 'cc-anchor-fail')(format=".")
:marked :marked
We'd need to write our anchor with a link array like this: We'd need to write our anchor with a link array like this:
+makeExample('router/ts/app/app.component.3.ts', 'cc-anchor-no-default')(format=".") +makeExample('router/ts/app/app.component.3.ts', 'cc-anchor-no-default')(format=".")
:marked :marked
Huh? *Crisis Center, Crisis Center*. This looks like a routing crisis! Let's parse it out.
But it actually makes sense. Let's parse it out.
* The first item in the array identifies the parent route ('CrisisCenter'). * The first item in the array identifies the parent route ('CrisisCenter').
* There are no parameters for this parent route so we're done with it. * There are no parameters for this parent route so we're done with it.
* There is no default for the child route so we need to pick one. * There is no default for the child route so we need to pick one.
* We decide to go to the `CrisisListComponent` whose route name just happens also to be 'CrisisCenter' * We decide to go to the `CrisisListComponent` whose route name is 'CrisisList'
* So we add that 'CrisisCenter' as the second item in the array. * So we add that 'CrisisList' as the second item in the array.
* Voila! `['CrisisCenter', 'CrisisCenter']`. * Voila! `['CrisisCenter', 'CrisisList']`.
Let's take it a step further. Let's take it a step further.
This time we'll build a link parameters array that navigates from the root of the application This time we'll build a link parameters array that navigates from the root of the application
@ -1325,7 +1327,7 @@ code-example(format="." language="bash").
It looks like this! It looks like this!
+makeExample('router/ts/app/app.component.3.ts', 'princess-anchor')(format=".") +makeExample('router/ts/app/app.component.3.ts', 'princess-anchor')(format=".")
:marked :marked
We could redefine our `AppComponent` template with *Crisis Center* routes exclusively If we wanted to, we could redefine our `AppComponent` template with *Crisis Center* routes exclusively:
+makeExample('router/ts/app/app.component.3.ts', 'template')(format=".") +makeExample('router/ts/app/app.component.3.ts', 'template')(format=".")
:marked :marked
### Link Parameters Arrays in Redirects ### Link Parameters Arrays in Redirects
@ -1342,7 +1344,7 @@ code-example(format="." language="bash").
Here's the redirect route we'll add to our configuration. Here's the redirect route we'll add to our configuration.
+makeExample('router/ts/app/app.component.ts', 'asteroid-route')(format=".") +makeExample('router/ts/app/app.component.ts', 'asteroid-route')(format=".")
:marked :marked
We hope the picture is clear. We can write applications with one, two or more levels of routing. In sum, we can write applications with one, two or more levels of routing.
The link parameters array affords the flexibility to represent any routing depth and The link parameters array affords the flexibility to represent any routing depth and
any legal sequence of route names and (optional) route parameter objects. any legal sequence of route names and (optional) route parameter objects.