docs: refactor routing doc (#35566)

This rewrite changes headings to focus on user tasks rather than features,
verifies that content is up-to-date and complete, removes colloquial phrases,
adds prerequisites, and expands on a task-based section in the beginning
(a quick reference).

PR Close #35566
This commit is contained in:
Kapunahele Wong 2020-02-19 17:43:26 -05:00 committed by atscott
parent 297b7da166
commit 305b059809
14 changed files with 1614 additions and 1714 deletions

View File

@ -0,0 +1,11 @@
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router'; // CLI imports router
const routes: Routes = []; // sets up routes constant where you define your routes
// configures NgModule imports and exports
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }

View File

@ -0,0 +1,26 @@
// #docplaster
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router'; // CLI imports router
// #docregion routes, routes-with-wildcard, redirect
const routes: Routes = [
{ path: 'first-component', component: FirstComponent },
{ path: 'second-component', component: SecondComponent },
// #enddocregion routes
{ path: '', redirectTo: '/first-component', pathMatch: 'full' }, // redirect to `first-component`
{ path: '**', component: FirstComponent },
// #enddocregion redirect
{ path: '**', component: PageNotFoundComponent }, // Wildcard route for a 404 page
// #docregion routes
// #docregion redirect
];
// #enddocregion routes, routes-with-wildcard, redirect
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }

View File

@ -0,0 +1,28 @@
// #docplaster
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router'; // CLI imports router
// #docregion child-routes
const routes: Routes = [
{ path: 'first-component',
component: FirstComponent, // this is the component with the <router-outlet> in the template
children: [
{
path: 'child-a', // child route path
component: ChildAComponent // child route component that the router renders
},
{
path: 'child-b',
component: ChildBComponent // another child route component that the router renders
}
] },
// #enddocregion child-routes
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }

View File

@ -0,0 +1,15 @@
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.css']
})
export class AppComponent {
// #docregion relative-to
goToItems() {
this.router.navigate(['items'], { relativeTo: this.route });
}
// #enddocregion relative-to
}

View File

@ -0,0 +1,10 @@
<h1>Angular Router App</h1>
<!-- This nav gives you links to click, which tells the router which route to use (defined in the routes constant in AppRoutingModule) -->
<nav>
<ul>
<li><a routerLink="/first-component" routerLinkActive="active">First Component</a></li>
<li><a routerLink="/second-component" routerLinkActive="active">Second Component</a></li>
</ul>
</nav>
<!-- The routed views render in the <router-outlet>-->
<router-outlet></router-outlet>

View File

@ -0,0 +1,26 @@
<!-- #docregion child-routes-->
<h2>First Component</h2>
<nav>
<ul>
<li><a routerLink="child-a">Child A</a></li>
<li><a routerLink="child-b">Child B</a></li>
</ul>
</nav>
<router-outlet></router-outlet>
<!-- #enddocregion child-routes-->
<!-- #docregion relative-route-->
<h2>First Component</h2>
<nav>
<ul>
<li><a routerLink="../second-component">Relative Route to second component</a></li>
</ul>
</nav>
<router-outlet></router-outlet>
<!-- #enddocregion relative-route-->

View File

@ -0,0 +1,17 @@
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module'; // CLI imports AppRoutingModule
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule // CLI adds AppRoutingModule to the AppModule's imports array
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

View File

@ -2,7 +2,9 @@
// #docregion // #docregion
import { switchMap } from 'rxjs/operators'; import { switchMap } from 'rxjs/operators';
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
// #docregion imports-route-info
import { Router, ActivatedRoute, ParamMap } from '@angular/router'; import { Router, ActivatedRoute, ParamMap } from '@angular/router';
// #enddocregion imports-route-info
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { HeroService } from '../hero.service'; import { HeroService } from '../hero.service';
@ -16,11 +18,16 @@ import { Hero } from '../hero';
export class HeroDetailComponent implements OnInit { export class HeroDetailComponent implements OnInit {
hero$: Observable<Hero>; hero$: Observable<Hero>;
// #docregion activated-route
constructor( constructor(
private route: ActivatedRoute, private route: ActivatedRoute,
// #enddocregion activated-route
private router: Router, private router: Router,
private service: HeroService private service: HeroService
// #docregion activated-route
) {} ) {}
// #enddocregion activated-route
ngOnInit() { ngOnInit() {
this.hero$ = this.route.paramMap.pipe( this.hero$ = this.route.paramMap.pipe(

View File

@ -415,8 +415,8 @@ The following are some of the key AngularJS built-in directives and their equiva
<code-example hideCopy path="ajs-quick-reference/src/app/app.component.html" region="router-link"></code-example> <code-example hideCopy path="ajs-quick-reference/src/app/app.component.html" region="router-link"></code-example>
For more information on routing, see the [RouterLink binding](guide/router#router-link) For more information on routing, see [Defining a basic route](guide/router#basic-route)
section of the [Routing & Navigation](guide/router) page. in the [Routing & Navigation](guide/router) page.
</td> </td>

View File

@ -303,7 +303,7 @@ Some features of Angular may require additional polyfills.
<td> <td>
[Router](guide/router) when using [Router](guide/router) when using
[hash-based routing](guide/router#appendix-locationstrategy-and-browser-url-styles) [hash-based routing](guide/router#location-strategy)
</td> </td>
<td> <td>

View File

@ -321,7 +321,7 @@ absolutely must be present when the app starts.
Configure the Angular Router to defer loading of all other modules (and their associated code), either by Configure the Angular Router to defer loading of all other modules (and their associated code), either by
[waiting until the app has launched](guide/router#preloading "Preloading") [waiting until the app has launched](guide/router#preloading "Preloading")
or by [_lazy loading_](guide/router#asynchronous-routing "Lazy loading") or by [_lazy loading_](guide/router#lazy-loading "Lazy loading")
them on demand. them on demand.
<div class="callout is-helpful"> <div class="callout is-helpful">

View File

@ -318,6 +318,7 @@ const routes: Routes = [{
{@a activatedroute-props} {@a activatedroute-props}
### ActivatedRoute params and queryParams properties ### ActivatedRoute params and queryParams properties
[ActivatedRoute](api/router/ActivatedRoute) contains two [properties](api/router/ActivatedRoute#properties) that are less capable than their replacements and may be deprecated in a future Angular version. [ActivatedRoute](api/router/ActivatedRoute) contains two [properties](api/router/ActivatedRoute#properties) that are less capable than their replacements and may be deprecated in a future Angular version.
@ -327,7 +328,7 @@ const routes: Routes = [{
| `params` | `paramMap` | | `params` | `paramMap` |
| `queryParams` | `queryParamMap` | | `queryParams` | `queryParamMap` |
For more information see the [Router guide](guide/router#activated-route). For more information see the [Getting route information](guide/router#activated-route) section of the [Router guide](guide/router).
{@a reflect-metadata} {@a reflect-metadata}

File diff suppressed because it is too large Load Diff

View File

@ -1958,7 +1958,7 @@ for the `id` to change during its lifetime.
<div class="alert is-helpful"> <div class="alert is-helpful">
The [Router](guide/router#route-parameters) guide covers `ActivatedRoute.paramMap` in more detail. The [ActivatedRoute in action](guide/router#activated-route-in-action) section of the [Router](guide/router) guide covers `ActivatedRoute.paramMap` in more detail.
</div> </div>