`,
- styles: ['input {width: 20em}'],
- animations: [ slideInDownAnimation ]
+ styles: ['input {width: 20em}']
})
export class CrisisDetailComponent implements OnInit {
- @HostBinding('@routeAnimation') routeAnimation = true;
- @HostBinding('style.display') display = 'block';
- @HostBinding('style.position') position = 'absolute';
-
crisis: Crisis;
editName: string;
diff --git a/aio/content/examples/router/src/app/crisis-center/crisis-detail.component.ts b/aio/content/examples/router/src/app/crisis-center/crisis-detail.component.ts
index 2b5150686c83..c3510a8df956 100644
--- a/aio/content/examples/router/src/app/crisis-center/crisis-detail.component.ts
+++ b/aio/content/examples/router/src/app/crisis-center/crisis-detail.component.ts
@@ -4,7 +4,6 @@ import { Component, OnInit, HostBinding } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Observable } from 'rxjs';
-import { slideInDownAnimation } from '../animations';
import { Crisis } from './crisis.service';
import { DialogService } from '../dialog.service';
@@ -24,14 +23,9 @@ import { DialogService } from '../dialog.service';
`,
- styles: ['input {width: 20em}'],
- animations: [ slideInDownAnimation ]
+ styles: ['input {width: 20em}']
})
export class CrisisDetailComponent implements OnInit {
- @HostBinding('@routeAnimation') routeAnimation = true;
- @HostBinding('style.display') display = 'block';
- @HostBinding('style.position') position = 'absolute';
-
crisis: Crisis;
editName: string;
diff --git a/aio/content/examples/router/src/app/heroes/hero-detail.component.ts b/aio/content/examples/router/src/app/heroes/hero-detail.component.ts
index 21706868644f..2ef9347dcfb3 100644
--- a/aio/content/examples/router/src/app/heroes/hero-detail.component.ts
+++ b/aio/content/examples/router/src/app/heroes/hero-detail.component.ts
@@ -7,8 +7,6 @@ import { Component, OnInit, HostBinding } from '@angular/core';
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
import { Observable } from 'rxjs';
-import { slideInDownAnimation } from '../animations';
-
import { Hero, HeroService } from './hero.service';
@Component({
@@ -26,16 +24,9 @@ import { Hero, HeroService } from './hero.service';
- `,
- animations: [ slideInDownAnimation ]
+ `
})
export class HeroDetailComponent implements OnInit {
-// #docregion host-bindings
- @HostBinding('@routeAnimation') routeAnimation = true;
- @HostBinding('style.display') display = 'block';
- @HostBinding('style.position') position = 'absolute';
-// #enddocregion host-bindings
-
hero$: Observable;
// #docregion ctor
diff --git a/aio/content/examples/router/src/app/heroes/heroes-routing.module.2.ts b/aio/content/examples/router/src/app/heroes/heroes-routing.module.2.ts
new file mode 100644
index 000000000000..6f8d5fda4b87
--- /dev/null
+++ b/aio/content/examples/router/src/app/heroes/heroes-routing.module.2.ts
@@ -0,0 +1,22 @@
+// #docregion
+import { NgModule } from '@angular/core';
+import { RouterModule, Routes } from '@angular/router';
+
+import { HeroListComponent } from './hero-list.component';
+import { HeroDetailComponent } from './hero-detail.component';
+
+const heroesRoutes: Routes = [
+ { path: 'heroes', component: HeroListComponent, data: { animation: 'heroes' } },
+ { path: 'hero/:id', component: HeroDetailComponent, data: { animation: 'hero' } }
+];
+
+@NgModule({
+ imports: [
+ RouterModule.forChild(heroesRoutes)
+ ],
+ exports: [
+ RouterModule
+ ]
+})
+export class HeroRoutingModule { }
+// #enddocregion
diff --git a/aio/content/examples/router/src/app/heroes/heroes-routing.module.ts b/aio/content/examples/router/src/app/heroes/heroes-routing.module.ts
index 43558907b08d..11aab5f48c57 100644
--- a/aio/content/examples/router/src/app/heroes/heroes-routing.module.ts
+++ b/aio/content/examples/router/src/app/heroes/heroes-routing.module.ts
@@ -8,8 +8,8 @@ import { HeroDetailComponent } from './hero-detail.component';
const heroesRoutes: Routes = [
{ path: 'heroes', redirectTo: '/superheroes' },
{ path: 'hero/:id', redirectTo: '/superhero/:id' },
- { path: 'superheroes', component: HeroListComponent },
- { path: 'superhero/:id', component: HeroDetailComponent }
+ { path: 'superheroes', component: HeroListComponent, data: { animation: 'heroes' } },
+ { path: 'superhero/:id', component: HeroDetailComponent, data: { animation: 'hero' } }
];
@NgModule({
diff --git a/aio/content/guide/router.md b/aio/content/guide/router.md
index 0ecd1c81515f..3802e1eb9f82 100644
--- a/aio/content/guide/router.md
+++ b/aio/content/guide/router.md
@@ -2171,8 +2171,7 @@ The optional `foo` route parameter is harmless and continues to be ignored.
### Adding animations to the routed component
The heroes feature module is almost complete, but what is a feature without some smooth transitions?
-This section shows you how to add some [animations](guide/animations)
-to the `HeroDetailComponent`.
+This section shows you how to add some [animations](guide/animations) to the `HeroDetailComponent`.
First import `BrowserAnimationsModule`:
@@ -2180,6 +2179,11 @@ First import `BrowserAnimationsModule`:
+Next, add a `data` object to the routes for `HeroListComponent` and `HeroDetailComponent`. Transitions are based on `states` and you'll use the `animation` data from the route to provide a named animation `state` for the transitions.
+
+
+
+
Create an `animations.ts` file in the root `src/app/` folder. The contents look like this:
@@ -2189,53 +2193,40 @@ Create an `animations.ts` file in the root `src/app/` folder. The contents look
-
This file does the following:
* Imports the animation symbols that build the animation triggers, control state, and manage transitions between states.
-* Exports a constant named `slideInDownAnimation` set to an animation trigger named *`routeAnimation`*;
-animated components will refer to this name.
-
-* Specifies the _wildcard state_ , `*`, that matches any animation state that the route component is in.
+* Exports a constant named `slideInAnimation` set to an animation trigger named *`routeAnimation`*;
-* Defines two *transitions*, one to ease the component in from the left of the screen as it enters the application view (`:enter`),
-the other to animate the component down as it leaves the application view (`:leave`).
+* Defines one *transition* when switching back and forth from the `heroes` and `hero` routes to ease the component in from the left of the screen as it enters the application view (`:enter`), the other to animate the component to the right as it leaves the application view (`:leave`).
-You could create more triggers with different transitions for other route components. This trigger is sufficient for the current milestone.
-
-
-Back in the `HeroDetailComponent`, import the `slideInDownAnimation` from `'./animations.ts`.
-Add the `HostBinding` decorator to the imports from `@angular/core`; you'll need it in a moment.
-
-Add an `animations` array to the `@Component` metadata's that contains the `slideInDownAnimation`.
+You could also create more transitions for other routes. This trigger is sufficient for the current milestone.
-Then add three `@HostBinding` properties to the class to set the animation and styles for the route component's element.
+Back in the `AppComponent`, import the `RouterOutlet` token from the `@angular/router` package and the `slideInDownAnimation` from `'./animations.ts`.
-
+
+In order to use the routable animations, you'll need to wrap the `RouterOutlet` inside an element. You'll
+use the `@routeAnimation` trigger and bind it to the element.
+For the `@routeAnimation` transitions to key off states, you'll need to provide it with the `data` from the `ActivatedRoute`. The `RouterOutlet` is exposed as an `outlet` template variable, so you bind a reference to the router outlet. A variable of `routerOutlet` is an ideal choice.
-The `'@routeAnimation'` passed to the first `@HostBinding` matches
-the name of the `slideInDownAnimation` _trigger_, `routeAnimation`.
-Set the `routeAnimation` property to `true` because you only care about the `:enter` and `:leave` states.
-
-The other two `@HostBinding` properties style the display and position of the component.
-
-The `HeroDetailComponent` will ease in from the left when routed to and will slide down when navigating away.
-
+Add an `animations` array to the `@Component` metadata's that contains the `slideInDownAnimation`.
-
+
+
+The `@routeAnimation` property is bound to the `getAnimationData` with the provided `routerOutlet` reference, so you'll need to define that function in the `AppComponent`. The `getAnimationData` function returns the animation property from the `data` provided through the `ActivatedRoute`. The `animation` property matches the `transition` names you used in the `slideDownAnimation` defined in `animations.ts`.
-Applying route animations to individual components works for a simple demo, but in a real life app,
-it is better to animate routes based on _route paths_.
+
+
-
- `
-})
-export class AdminDashboardComponent { }
diff --git a/aio/content/examples/router/src/app/admin/admin-dashboard/admin-dashboard.component.1.html b/aio/content/examples/router/src/app/admin/admin-dashboard/admin-dashboard.component.1.html
new file mode 100644
index 000000000000..754a2621543c
--- /dev/null
+++ b/aio/content/examples/router/src/app/admin/admin-dashboard/admin-dashboard.component.1.html
@@ -0,0 +1 @@
+
Dashboard
\ No newline at end of file
diff --git a/aio/content/examples/router/src/app/admin/admin-dashboard.component.2.ts b/aio/content/examples/router/src/app/admin/admin-dashboard/admin-dashboard.component.1.ts
similarity index 82%
rename from aio/content/examples/router/src/app/admin/admin-dashboard.component.2.ts
rename to aio/content/examples/router/src/app/admin/admin-dashboard/admin-dashboard.component.1.ts
index 19406bea9b6e..8806d7cea845 100644
--- a/aio/content/examples/router/src/app/admin/admin-dashboard.component.2.ts
+++ b/aio/content/examples/router/src/app/admin/admin-dashboard/admin-dashboard.component.1.ts
@@ -5,13 +5,9 @@ import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@Component({
- template: `
-
\ No newline at end of file
diff --git a/aio/content/examples/router/src/app/admin/admin-dashboard/admin-dashboard.component.css b/aio/content/examples/router/src/app/admin/admin-dashboard/admin-dashboard.component.css
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/aio/content/examples/router/src/app/admin/admin-dashboard/admin-dashboard.component.html b/aio/content/examples/router/src/app/admin/admin-dashboard/admin-dashboard.component.html
new file mode 100644
index 000000000000..9c14d5b26605
--- /dev/null
+++ b/aio/content/examples/router/src/app/admin/admin-dashboard/admin-dashboard.component.html
@@ -0,0 +1,10 @@
+
Dashboard
+
+
Session ID: {{ sessionId | async }}
+
+
Token: {{ token | async }}
+
+Preloaded Modules
+
+
{{ module }}
+
\ No newline at end of file
diff --git a/aio/content/examples/router/src/app/admin/admin-dashboard.component.ts b/aio/content/examples/router/src/app/admin/admin-dashboard/admin-dashboard.component.ts
similarity index 67%
rename from aio/content/examples/router/src/app/admin/admin-dashboard.component.ts
rename to aio/content/examples/router/src/app/admin/admin-dashboard/admin-dashboard.component.ts
index 11be8fd02926..2447f3ad9c8a 100644
--- a/aio/content/examples/router/src/app/admin/admin-dashboard.component.ts
+++ b/aio/content/examples/router/src/app/admin/admin-dashboard/admin-dashboard.component.ts
@@ -4,22 +4,12 @@ import { ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
-import { SelectivePreloadingStrategy } from '../selective-preloading-strategy';
-
+import { SelectivePreloadingStrategyService } from '../../selective-preloading-strategy.service';
@Component({
- template: `
-
Dashboard
-
-
Session ID: {{ sessionId | async }}
-
-
Token: {{ token | async }}
-
- Preloaded Modules
-
-
{{ module }}
-
- `
+ selector: 'app-admin-dashboard',
+ templateUrl: './admin-dashboard.component.html',
+ styleUrls: ['./admin-dashboard.component.css']
})
export class AdminDashboardComponent implements OnInit {
sessionId: Observable;
@@ -28,7 +18,7 @@ export class AdminDashboardComponent implements OnInit {
constructor(
private route: ActivatedRoute,
- private preloadStrategy: SelectivePreloadingStrategy
+ preloadStrategy: SelectivePreloadingStrategyService
) {
this.modules = preloadStrategy.preloadedModules;
}
diff --git a/aio/content/examples/router/src/app/admin/admin-routing.module.1.ts b/aio/content/examples/router/src/app/admin/admin-routing.module.1.ts
index e7d83f113f62..6c3f6cbfc429 100644
--- a/aio/content/examples/router/src/app/admin/admin-routing.module.1.ts
+++ b/aio/content/examples/router/src/app/admin/admin-routing.module.1.ts
@@ -3,10 +3,10 @@
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
-import { AdminComponent } from './admin.component';
-import { AdminDashboardComponent } from './admin-dashboard.component';
-import { ManageCrisesComponent } from './manage-crises.component';
-import { ManageHeroesComponent } from './manage-heroes.component';
+import { AdminComponent } from './admin/admin.component';
+import { AdminDashboardComponent } from './admin-dashboard/admin-dashboard.component';
+import { ManageCrisesComponent } from './manage-crises/manage-crises.component';
+import { ManageHeroesComponent } from './manage-heroes/manage-heroes.component';
// #docregion admin-routes
const adminRoutes: Routes = [
diff --git a/aio/content/examples/router/src/app/admin/admin-routing.module.2.ts b/aio/content/examples/router/src/app/admin/admin-routing.module.2.ts
index d945201afed1..9b80c2439040 100644
--- a/aio/content/examples/router/src/app/admin/admin-routing.module.2.ts
+++ b/aio/content/examples/router/src/app/admin/admin-routing.module.2.ts
@@ -3,13 +3,13 @@
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
-import { AdminComponent } from './admin.component';
-import { AdminDashboardComponent } from './admin-dashboard.component';
-import { ManageCrisesComponent } from './manage-crises.component';
-import { ManageHeroesComponent } from './manage-heroes.component';
+import { AdminComponent } from './admin/admin.component';
+import { AdminDashboardComponent } from './admin-dashboard/admin-dashboard.component';
+import { ManageCrisesComponent } from './manage-crises/manage-crises.component';
+import { ManageHeroesComponent } from './manage-heroes/manage-heroes.component';
// #docregion admin-route
-import { AuthGuard } from '../auth-guard.service';
+import { AuthGuard } from '../auth/auth.guard';
const adminRoutes: Routes = [
{
diff --git a/aio/content/examples/router/src/app/admin/admin-routing.module.3.ts b/aio/content/examples/router/src/app/admin/admin-routing.module.3.ts
index 63f1c9aaf4d9..b337cab0b5b7 100644
--- a/aio/content/examples/router/src/app/admin/admin-routing.module.3.ts
+++ b/aio/content/examples/router/src/app/admin/admin-routing.module.3.ts
@@ -3,13 +3,13 @@
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
-import { AdminComponent } from './admin.component';
-import { AdminDashboardComponent } from './admin-dashboard.component';
-import { ManageCrisesComponent } from './manage-crises.component';
-import { ManageHeroesComponent } from './manage-heroes.component';
+import { AdminComponent } from './admin/admin.component';
+import { AdminDashboardComponent } from './admin-dashboard/admin-dashboard.component';
+import { ManageCrisesComponent } from './manage-crises/manage-crises.component';
+import { ManageHeroesComponent } from './manage-heroes/manage-heroes.component';
// #docregion admin-route
-import { AuthGuard } from '../auth-guard.service';
+import { AuthGuard } from '../auth/auth.guard';
// #docregion can-activate-child
const adminRoutes: Routes = [
diff --git a/aio/content/examples/router/src/app/admin/admin-routing.module.ts b/aio/content/examples/router/src/app/admin/admin-routing.module.ts
index 2b1048d110f7..6faa557b16a6 100644
--- a/aio/content/examples/router/src/app/admin/admin-routing.module.ts
+++ b/aio/content/examples/router/src/app/admin/admin-routing.module.ts
@@ -3,12 +3,12 @@
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
-import { AdminComponent } from './admin.component';
-import { AdminDashboardComponent } from './admin-dashboard.component';
-import { ManageCrisesComponent } from './manage-crises.component';
-import { ManageHeroesComponent } from './manage-heroes.component';
+import { AdminComponent } from './admin/admin.component';
+import { AdminDashboardComponent } from './admin-dashboard/admin-dashboard.component';
+import { ManageCrisesComponent } from './manage-crises/manage-crises.component';
+import { ManageHeroesComponent } from './manage-heroes/manage-heroes.component';
-import { AuthGuard } from '../auth-guard.service';
+import { AuthGuard } from '../auth/auth.guard';
const adminRoutes: Routes = [
{
diff --git a/aio/content/examples/router/src/app/admin/admin.component.ts b/aio/content/examples/router/src/app/admin/admin.component.ts
deleted file mode 100644
index 30abfa452480..000000000000
--- a/aio/content/examples/router/src/app/admin/admin.component.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-// #docregion
-import { Component } from '@angular/core';
-
-@Component({
- template: `
-
ADMIN
-
-
- `
-})
-export class AdminComponent {
-}
diff --git a/aio/content/examples/router/src/app/admin/admin.module.ts b/aio/content/examples/router/src/app/admin/admin.module.ts
index 2736f00e1d7a..d276b0efd5a7 100644
--- a/aio/content/examples/router/src/app/admin/admin.module.ts
+++ b/aio/content/examples/router/src/app/admin/admin.module.ts
@@ -2,10 +2,10 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
-import { AdminComponent } from './admin.component';
-import { AdminDashboardComponent } from './admin-dashboard.component';
-import { ManageCrisesComponent } from './manage-crises.component';
-import { ManageHeroesComponent } from './manage-heroes.component';
+import { AdminComponent } from './admin/admin.component';
+import { AdminDashboardComponent } from './admin-dashboard/admin-dashboard.component';
+import { ManageCrisesComponent } from './manage-crises/manage-crises.component';
+import { ManageHeroesComponent } from './manage-heroes/manage-heroes.component';
import { AdminRoutingModule } from './admin-routing.module';
diff --git a/aio/content/examples/router/src/app/admin/admin/admin.component.css b/aio/content/examples/router/src/app/admin/admin/admin.component.css
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/aio/content/examples/router/src/app/admin/admin/admin.component.html b/aio/content/examples/router/src/app/admin/admin/admin.component.html
new file mode 100644
index 000000000000..6e924c35d6de
--- /dev/null
+++ b/aio/content/examples/router/src/app/admin/admin/admin.component.html
@@ -0,0 +1,8 @@
+
ADMIN
+
+
\ No newline at end of file
diff --git a/aio/content/examples/router/src/app/admin/admin/admin.component.ts b/aio/content/examples/router/src/app/admin/admin/admin.component.ts
new file mode 100644
index 000000000000..beb6b89fcf92
--- /dev/null
+++ b/aio/content/examples/router/src/app/admin/admin/admin.component.ts
@@ -0,0 +1,10 @@
+// #docregion
+import { Component } from '@angular/core';
+
+@Component({
+ selector: 'app-admin',
+ templateUrl: './admin.component.html',
+ styleUrls: ['./admin.component.css']
+})
+export class AdminComponent {
+}
diff --git a/aio/content/examples/router/src/app/admin/manage-crises.component.ts b/aio/content/examples/router/src/app/admin/manage-crises.component.ts
deleted file mode 100644
index d3176563ebce..000000000000
--- a/aio/content/examples/router/src/app/admin/manage-crises.component.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-// #docregion
-import { Component } from '@angular/core';
-
-@Component({
- template: `
-
Manage your crises here
- `
-})
-export class ManageCrisesComponent { }
diff --git a/aio/content/examples/router/src/app/admin/manage-crises/manage-crises.component.css b/aio/content/examples/router/src/app/admin/manage-crises/manage-crises.component.css
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/aio/content/examples/router/src/app/admin/manage-crises/manage-crises.component.html b/aio/content/examples/router/src/app/admin/manage-crises/manage-crises.component.html
new file mode 100644
index 000000000000..4edfa721335a
--- /dev/null
+++ b/aio/content/examples/router/src/app/admin/manage-crises/manage-crises.component.html
@@ -0,0 +1 @@
+
Manage your crises here
\ No newline at end of file
diff --git a/aio/content/examples/router/src/app/admin/manage-crises/manage-crises.component.ts b/aio/content/examples/router/src/app/admin/manage-crises/manage-crises.component.ts
new file mode 100644
index 000000000000..c3fe3bb92d00
--- /dev/null
+++ b/aio/content/examples/router/src/app/admin/manage-crises/manage-crises.component.ts
@@ -0,0 +1,9 @@
+// #docregion
+import { Component } from '@angular/core';
+
+@Component({
+ selector: 'app-manage-crises',
+ templateUrl: './manage-crises.component.html',
+ styleUrls: ['./manage-crises.component.css']
+})
+export class ManageCrisesComponent { }
diff --git a/aio/content/examples/router/src/app/admin/manage-heroes.component.ts b/aio/content/examples/router/src/app/admin/manage-heroes.component.ts
deleted file mode 100644
index 7f3a39893dae..000000000000
--- a/aio/content/examples/router/src/app/admin/manage-heroes.component.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-// #docregion
-import { Component } from '@angular/core';
-
-@Component({
- template: `
-
Manage your heroes here
- `
-})
-export class ManageHeroesComponent { }
diff --git a/aio/content/examples/router/src/app/admin/manage-heroes/manage-heroes.component.css b/aio/content/examples/router/src/app/admin/manage-heroes/manage-heroes.component.css
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/aio/content/examples/router/src/app/admin/manage-heroes/manage-heroes.component.html b/aio/content/examples/router/src/app/admin/manage-heroes/manage-heroes.component.html
new file mode 100644
index 000000000000..3e5256527d8d
--- /dev/null
+++ b/aio/content/examples/router/src/app/admin/manage-heroes/manage-heroes.component.html
@@ -0,0 +1 @@
+
'
-})
-export class PageNotFoundComponent {}
diff --git a/aio/content/examples/router/src/app/page-not-found/page-not-found.component.css b/aio/content/examples/router/src/app/page-not-found/page-not-found.component.css
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/aio/content/examples/router/src/app/page-not-found/page-not-found.component.html b/aio/content/examples/router/src/app/page-not-found/page-not-found.component.html
new file mode 100644
index 000000000000..6c581c4fc8ee
--- /dev/null
+++ b/aio/content/examples/router/src/app/page-not-found/page-not-found.component.html
@@ -0,0 +1 @@
+
Page not found
\ No newline at end of file
diff --git a/aio/content/examples/router/src/app/page-not-found/page-not-found.component.spec.ts b/aio/content/examples/router/src/app/page-not-found/page-not-found.component.spec.ts
new file mode 100644
index 000000000000..697a9465727a
--- /dev/null
+++ b/aio/content/examples/router/src/app/page-not-found/page-not-found.component.spec.ts
@@ -0,0 +1,25 @@
+import { async, ComponentFixture, TestBed } from '@angular/core/testing';
+
+import { PageNotFoundComponent } from './page-not-found.component';
+
+describe('PageNotFoundComponent', () => {
+ let component: PageNotFoundComponent;
+ let fixture: ComponentFixture;
+
+ beforeEach(async(() => {
+ TestBed.configureTestingModule({
+ declarations: [ PageNotFoundComponent ]
+ })
+ .compileComponents();
+ }));
+
+ beforeEach(() => {
+ fixture = TestBed.createComponent(PageNotFoundComponent);
+ component = fixture.componentInstance;
+ fixture.detectChanges();
+ });
+
+ it('should create', () => {
+ expect(component).toBeTruthy();
+ });
+});
diff --git a/aio/content/examples/router/src/app/page-not-found/page-not-found.component.ts b/aio/content/examples/router/src/app/page-not-found/page-not-found.component.ts
new file mode 100644
index 000000000000..c5c55a794dd2
--- /dev/null
+++ b/aio/content/examples/router/src/app/page-not-found/page-not-found.component.ts
@@ -0,0 +1,15 @@
+import { Component, OnInit } from '@angular/core';
+
+@Component({
+ selector: 'app-page-not-found',
+ templateUrl: './page-not-found.component.html',
+ styleUrls: ['./page-not-found.component.css']
+})
+export class PageNotFoundComponent implements OnInit {
+
+ constructor() { }
+
+ ngOnInit() {
+ }
+
+}
diff --git a/aio/content/examples/router/src/app/selective-preloading-strategy.ts b/aio/content/examples/router/src/app/selective-preloading-strategy.service.ts
similarity index 83%
rename from aio/content/examples/router/src/app/selective-preloading-strategy.ts
rename to aio/content/examples/router/src/app/selective-preloading-strategy.service.ts
index c2192ec12b2d..b1065b78af21 100644
--- a/aio/content/examples/router/src/app/selective-preloading-strategy.ts
+++ b/aio/content/examples/router/src/app/selective-preloading-strategy.service.ts
@@ -3,8 +3,10 @@ import { Injectable } from '@angular/core';
import { PreloadingStrategy, Route } from '@angular/router';
import { Observable, of } from 'rxjs';
-@Injectable()
-export class SelectivePreloadingStrategy implements PreloadingStrategy {
+@Injectable({
+ providedIn: 'root'
+})
+export class SelectivePreloadingStrategyService implements PreloadingStrategy {
preloadedModules: string[] = [];
preload(route: Route, load: () => Observable): Observable {
diff --git a/aio/content/examples/router/src/index.html b/aio/content/examples/router/src/index.html
index 0e5ca721fa9a..edb919d1ec97 100644
--- a/aio/content/examples/router/src/index.html
+++ b/aio/content/examples/router/src/index.html
@@ -9,7 +9,6 @@
Angular Router
-
diff --git a/aio/content/examples/router/stackblitz.json b/aio/content/examples/router/stackblitz.json
index c1f330ae39b9..4ffcac610bcc 100644
--- a/aio/content/examples/router/stackblitz.json
+++ b/aio/content/examples/router/stackblitz.json
@@ -4,8 +4,8 @@
"!**/*.d.ts",
"!**/*.js",
"!**/*.[0-9].*",
- "!src/app/crisis-list.component.ts",
- "!src/app/hero-list.component.ts"
+ "!src/app/crisis-list/*.*",
+ "!src/app/hero-list/*.*"
],
"tags": ["router"]
}
diff --git a/aio/content/guide/router.md b/aio/content/guide/router.md
index 3802e1eb9f82..8bd3bedd62d1 100644
--- a/aio/content/guide/router.md
+++ b/aio/content/guide/router.md
@@ -150,18 +150,20 @@ If you need to see what events are happening during the navigation lifecycle, th
Given this configuration, when the browser URL for this application becomes `/heroes`,
the router matches that URL to the route path `/heroes` and displays the `HeroListComponent`
-_after_ a `RouterOutlet` that you've placed in the host view's HTML.
+_after_ a `RouterOutlet` that you've placed in the host component's template.
+
+The `RouterOutlet` is a directive from the router library that marks
+the spot in the template where the router should display the components for that outlet.
<router-outlet></router-outlet>
- <!-- Routed views go here -->
+ <!-- Routed components go here -->
-
-
{@a basics-router-links}
+{@a router-link}
### Router links
@@ -174,12 +176,10 @@ an anchor tag.
Consider the following template:
-
+
-
-
The `RouterLink` directives on the anchor tags give the router control over those elements.
The navigation paths are fixed, so you can assign a string to the `routerLink` (a "one-time" binding).
@@ -187,9 +187,25 @@ Had the navigation path been more dynamic, you could have bound to a template ex
returned an array of route link parameters (the _link parameters array_).
The router resolves that array into a complete URL.
+
+{@a router-link-active}
+
+
+### Active Router Links
+
The **`RouterLinkActive`** directive on each anchor tag helps visually distinguish the anchor for the currently selected "active" route.
-The router adds the `active` CSS class to the element when the associated *RouterLink* becomes active.
-You can add this directive to the anchor or to its parent element.
+
+On each anchor tag, you also see [property bindings](guide/template-syntax#property-binding) to the `RouterLinkActive` directive that look like `routerLinkActive="..."`.
+
+The template expression to the right of the equals (=) contains a space-delimited string of CSS classes
+that the Router will add when this link is active (and remove when the link is inactive).
+You can also set the `RouterLinkActive` directive to a string of classes such as `[routerLinkActive]="'active fluffy'"`
+or bind it to a component property that returns such a string.
+
+The `RouterLinkActive` directive toggles css classes for active `RouterLink`s based on the current `RouterState`.
+This cascades down through each level of the route tree, so parent and child router links can be active at the same time.
+To override this behavior, you can bind to the `[routerLinkActiveOptions]` input binding with the `{ exact: true }` expression.
+By using `{ exact: true }`, a given `RouterLink` will only be active if its URL is an exact match to the current URL.
{@a basics-router-state}
@@ -373,6 +389,29 @@ During each navigation, the `Router` emits navigation events through the `Router
+
+
+ RouteConfigLoadStart
+
+
+
+ An [event](api/router/RouteConfigLoadStart) triggered before the `Router`
+ [lazy loads](#asynchronous-routing) a route configuration.
+
+
+
+
+
+
+ RouteConfigLoadEnd
+
+
+
+ An [event](api/router/RouteConfigLoadEnd) triggered after a route has been lazy loaded.
+
+
+
+
RoutesRecognized
@@ -386,23 +425,68 @@ During each navigation, the `Router` emits navigation events through the `Router
- RouteConfigLoadStart
+ GuardsCheckStart
- An [event](api/router/RouteConfigLoadStart) triggered before the `Router`
- [lazy loads](#asynchronous-routing) a route configuration.
+ An [event](api/router/GuardsCheckStart) triggered when the Router begins the Guards phase of routing.
- RouteConfigLoadEnd
+ ChildActivationStart
- An [event](api/router/RouteConfigLoadEnd) triggered after a route has been lazy loaded.
+ An [event](api/router/ChildActivationStart) triggered when the Router begins activating a route's children. This
+ event receives the Route config for the parent route.
+
+
+
+
+
+
+ GuardsCheckEnd
+
+
+
+ An [event](api/router/GuardsCheckEnd) triggered when the Router finishes the Guards phase of routing successfully.
+
+
+
+
+
+
+ ResolveStart
+
+
+
+ An [event](api/router/ResolveStart) triggered when the Router begins the Resolve phase of routing.
+
+
+
+
+
+
+ ResolveEnd
+
+
+
+ An [event](api/router/ResolveEnd) triggered when the Router finishes the Resolve phase of routing successfuly.
+
+
+
+
+
+
+ ChildActivationEnd
+
+
+
+ An [event](api/router/ChildActivationEnd) triggered when the Router finishes activating a route's children. This
+ event receives the Route config for the parent route.
@@ -440,9 +524,20 @@ During each navigation, the `Router` emits navigation events through the `Router
+
+
+
+ Scroll
+
+
+
+ An [event](api/router/Scroll) that represents a scrolling event.
+
+
+
-These events are logged to the console when the `enableTracing` option is enabled also. Since the events are provided as an `Observable`, you can `filter()` for events of interest and `subscribe()` to them to make decisions based on the sequence of events in the navigation process.
+These events are logged to the console when the `enableTracing` option is enabled also. Because the events are provided as an `Observable`, you can `filter()` for events of interest and `subscribe()` to them to make decisions based on the sequence of events in the navigation process.
{@a basics-summary}
@@ -692,7 +787,7 @@ Now click the *Crisis Center* link for a list of ongoing crises.
Select a crisis and the application takes you to a crisis editing screen.
-The _Crisis Detail_ appears in a child view on the same page, beneath the list.
+The _Crisis Detail_ appears in a child component on the same page, beneath the list.
Alter the name of a crisis.
Notice that the corresponding name in the crisis list does _not_ change.
@@ -732,112 +827,27 @@ Proceed to the first application milestone.
{@a getting-started}
-## Milestone 1: Getting started with the router
+## Milestone 1: Getting started
Begin with a simple version of the app that navigates between two empty views.
+
-
-
-{@a base-href}
-
-
-### Set the *<base href>*
-
-The router uses the browser's
-history.pushState
-for navigation. Thanks to `pushState`, you can make in-app URL paths look the way you want them to
-look, e.g. `localhost:3000/crisis-center`. The in-app URLs can be indistinguishable from server URLs.
-
-Modern HTML5 browsers were the first to support `pushState` which is why many people refer to these URLs as
-"HTML5 style" URLs.
-
-
-
-
-
-
-HTML5 style navigation is the router default.
-In the [LocationStrategy and browser URL styles](#browser-url-styles) Appendix,
-learn why HTML5 style is preferred, how to adjust its behavior, and how to switch to the
-older hash (#) style, if necessary.
-
-
-
-
-
-
-You must **add a
-<base href> element**
-to the app's `index.html` for `pushState` routing to work.
-The browser uses the `` value to prefix *relative* URLs when referencing
-CSS files, scripts, and images.
-
-Add the `` element just after the `` tag.
-If the `app` folder is the application root, as it is for this application,
-set the `href` value in **`index.html`** *exactly* as shown here.
-
-
-
-
-
-
-
-
-
-
-
-
-
- Live example note
-
-
-
-
-A live coding environment like Stackblitz sets the application base address dynamically so you can't specify a fixed address.
-That's why the example code replaces the `` with a script that writes the `` tag on the fly.
-
-
-
- <script>document.write('<base href="' + document.location + '" />');</script>
-
-
-
-
-
-You only need this trick for the live example, not production code.
-
-
-
-
-
-
{@a import}
+Generate a sample application to follow the walkthrough.
-### Importing from the router library
-
-Begin by importing some symbols from the router library.
-The Router is in its own `@angular/router` package.
-It's not part of the Angular core. The router is an optional service because not all applications
-need routing and, depending on your requirements, you may need a different routing library.
-
-You teach the router how to navigate by configuring it with routes.
-
-
-{@a route-config}
-
+
+ ng new angular-router-sample
+
-#### Define routes
+### Define Routes
A router must be configured with a list of route definitions.
-The first configuration defines an array of two routes with simple paths leading to the
-`CrisisListComponent` and `HeroListComponent`.
-
Each definition translates to a [Route](api/router/Route) object which has two things: a
`path`, the URL path segment for this route; and a
`component`, the component associated with this route.
@@ -854,155 +864,75 @@ the router activates an instance of the `CrisisListComponent` and displays its v
activates an instance of `CrisisListComponent`, displays its view, and updates the
browser's address location and history with the URL for that path.
+The first configuration defines an array of two routes with simple paths leading to the
+`CrisisListComponent` and `HeroListComponent`. Generate the `CrisisList` and `HeroList` components.
-Here is the first configuration. Pass the array of routes, `appRoutes`, to the `RouterModule.forRoot` method.
-It returns a module, containing the configured `Router` service provider, plus other providers that the routing library requires.
-Once the application is bootstrapped, the `Router` performs the initial navigation based on the current browser URL.
-
-
-
-
+
+ ng generate component crisis-list
-
-
-
-
-
-
-Adding the configured `RouterModule` to the `AppModule` is sufficient for simple route configurations.
-As the application grows, you'll want to refactor the routing configuration into a separate file
-and create a **[Routing Module](#routing-module)**, a special type of `Service Module` dedicated to the purpose
-of routing in feature modules.
-
-
-
-
-
-
-Providing the `RouterModule` in the `AppModule` makes the Router available everywhere in the application.
-
-
-{@a shell}
-
-
-### The *AppComponent* shell
-
-The root `AppComponent` is the application shell. It has a title, a navigation bar with two links,
-and a *router outlet* where the router swaps views on and off the page. Here's what you get:
-
-
-
-
-
-
-
-
-{@a shell-template}
-
-
-The corresponding component template looks like this:
-
-
-
-
+
+ ng generate component hero-list
+Replace the contents of each component with the sample HTML below.
+
-{@a router-outlet}
-
-
-### *RouterOutlet*
-
-The `RouterOutlet` is a directive from the router library that marks
-the spot in the template where the router should display the views for that outlet.
-
-
-
-
-
-
-The router adds the `` element to the DOM
-and subsequently inserts the navigated view element
-immediately _after_ the ``.
-
+
-
+
+
+
-{@a router-link}
+
+### Register Router and Routes
-### *RouterLink* binding
+In order to use the Router, you must first register the `RouterModule` from the `@angular/router` package. Define an array of routes, `appRoutes`, and pass them to the `RouterModule.forRoot` method. It returns a module, containing the configured `Router` service provider, plus other providers that the routing library requires. Once the application is bootstrapped, the `Router` performs the initial navigation based on the current browser URL.
-Above the outlet, within the anchor tags, you see
-[attribute bindings](guide/template-syntax#attribute-binding) to
-the `RouterLink` directive that look like `routerLink="..."`.
-The links in this example each have a string path, the path of a route that
-you configured earlier. There are no route parameters yet.
+
-You can also add more contextual information to the `RouterLink` by providing query string parameters
-or a URL fragment for jumping to different areas on the page. Query string parameters
-are provided through the `[queryParams]` binding which takes an object (e.g. `{ name: 'value' }`), while the URL fragment
-takes a single value bound to the `[fragment]` input binding.
+
-
-
-Learn about the how you can also use the _link parameters array_ in the [appendix below](#link-parameters-array).
-
+Adding the configured `RouterModule` to the `AppModule` is sufficient for simple route configurations. As the application grows, you'll want to refactor the routing configuration into a separate file and create a **[Routing Module](#routing-module)**, a special type of `Service Module` dedicated to the purpose of routing in feature modules.
+Providing the `RouterModule` in the `AppModule` makes the Router available everywhere in the application.
+{@a shell}
-{@a router-link-active}
-
-
-### *RouterLinkActive* binding
-
-On each anchor tag, you also see [property bindings](guide/template-syntax#property-binding) to
-the `RouterLinkActive` directive that look like `routerLinkActive="..."`.
-
-The template expression to the right of the equals (=) contains a space-delimited string of CSS classes
-that the Router will add when this link is active (and remove when the link is inactive).
-You can also set the `RouterLinkActive` directive to a string of classes such as `[routerLinkActive]="'active fluffy'"`
-or bind it to a component property that returns such a string.
-
-The `RouterLinkActive` directive toggles css classes for active `RouterLink`s based on the current `RouterState`.
-This cascades down through each level of the route tree, so parent and child router links can be active at the same time.
-To override this behavior, you can bind to the `[routerLinkActiveOptions]` input binding with the `{ exact: true }` expression.
-By using `{ exact: true }`, a given `RouterLink` will only be active if its URL is an exact match to the current URL.
+### Add the Router Outlet
-{@a router-directives}
+The root `AppComponent` is the application shell. It has a title, a navigation bar with two links, and a *router outlet* where the router swaps views on and off the page. Here's what you get:
-### *Router directives*
+
+
+
-`RouterLink`, `RouterLinkActive` and `RouterOutlet` are directives provided by the Angular `RouterModule` package.
-They are readily available for you to use in the template.
+The `router-outlet` serves as a placeholder when the routed components will be rendered below it.
-The current state of `app.component.ts` looks like this:
+{@a shell-template}
+The corresponding component template looks like this:
-
+
-
-
{@a wildcard}
+### Define a Wildcard route
-### Wildcard route
-
-You've created two routes in the app so far, one to `/crisis-center` and the other to `/heroes`.
-Any other URL causes the router to throw an error and crash the app.
+You've created two routes in the app so far, one to `/crisis-center` and the other to `/heroes`. Any other URL causes the router to throw an error and crash the app.
Add a **wildcard** route to intercept invalid URLs and handle them gracefully.
A _wildcard_ route has a path consisting of two asterisks. It matches _every_ URL.
@@ -1012,25 +942,18 @@ A wildcard route can navigate to a custom "404 Not Found" component or [redirect
-
-
The router selects the route with a [_first match wins_](#example-config) strategy.
Wildcard routes are the least specific routes in the route configuration.
Be sure it is the _last_ route in the configuration.
-
-
-
To test this feature, add a button with a `RouterLink` to the `HeroListComponent` template and set the link to `"/sidekicks"`.
-
+
-
-
The application will fail if the user clicks that button because you haven't defined a `"/sidekicks"` route yet.
Instead of adding the `"/sidekicks"` route, define a `wildcard` route instead and have it navigate to a simple `PageNotFoundComponent`.
@@ -1039,32 +962,27 @@ Instead of adding the `"/sidekicks"` route, define a `wildcard` route instead an
-
-
Create the `PageNotFoundComponent` to display when users visit invalid URLs.
-
-
+
+ ng generate component page-not-found
+
-
-As with the other components, add the `PageNotFoundComponent` to the `AppModule` declarations.
+
Now when the user visits `/sidekicks`, or any other invalid URL, the browser displays "Page not found".
The browser address bar continues to point to the invalid URL.
+{@a redirect}
-
-{@a default-route}
-
-
-### The _default_ route to heroes
+### Set up redirects
When the application launches, the initial URL in the browser bar is something like:
- localhost:3000
+ localhost:4200
That doesn't match any of the concrete configured routes which means
@@ -1072,11 +990,7 @@ the router falls through to the wildcard route and displays the `PageNotFoundCom
The application needs a **default route** to a valid page.
The default page for this app is the list of heroes.
-The app should navigate there as if the user clicked the "Heroes" link or pasted `localhost:3000/heroes` into the address bar.
-
-{@a redirect}
-
-### Redirecting routes
+The app should navigate there as if the user clicked the "Heroes" link or pasted `localhost:4200/heroes` into the address bar.
The preferred solution is to add a `redirect` route that translates the initial relative URL (`''`)
to the desired default path (`/heroes`). The browser address bar shows `.../heroes` as if you'd navigated there directly.
@@ -1089,7 +1003,6 @@ It's just above the wildcard route in the following excerpt showing the complete
-
A redirect route requires a `pathMatch` property to tell the router how to match a URL to the path of a route.
The router throws an error if you don't.
In this app, the router should select the route to the `HeroListComponent` only when the *entire URL* matches `''`,
@@ -1099,7 +1012,6 @@ so set the `pathMatch` value to `'full'`.
-
Technically, `pathMatch = 'full'` results in a route hit when the *remaining*, unmatched segments of the URL match `''`.
In this example, the redirect is in a top level route so the *remaining* URL and the *entire* URL are the same thing.
@@ -1127,7 +1039,6 @@ Learn more in Victor Savkin's
-
### Basics wrap up
You've got a very basic navigating app, one that can switch between two views
@@ -1143,16 +1054,12 @@ You've learned how to do the following:
* handle invalid routes with a `wildcard` route.
* navigate to the default route when the app launches with an empty path.
-The rest of the starter app is mundane, with little interest from a router perspective.
-Here are the details for readers inclined to build the sample through to this milestone.
-
The starter app's structure looks like this:
-
- router-sample
+ angular-router-sample
@@ -1170,23 +1077,97 @@ The starter app's structure looks like this:
- app.component.ts
+ crisis-list
+
+
+
+
+
+
+ crisis-list.component.css
+
+
+
+
+
+ crisis-list.component.html
+
+
+
+
+
+ crisis-list.component.ts
+
+
+
- app.module.ts
+ hero-list
+
+
+
+
+
+
+ hero-list.component.css
+
+
+
+
+
+ hero-list.component.html
+
+
+
+
+
+ hero-list.component.ts
+
+
+
- crisis-list.component.ts
+ page-not-found
+
+
+
+
+
+
+ page-not-found.component.css
+
+
+
+
+
+ page-not-found.component.html
+
+
+
+
+
+ page-not-found.component.ts
+
+
+
- hero-list.component.ts
+ app.component.css
+
+
+
+ app.component.html
+
+
+
+ app.component.ts
- not-found.component.ts
+ app.module.ts
@@ -1228,7 +1209,7 @@ Here are the files discussed in this milestone.
-
+
@@ -1236,19 +1217,15 @@ Here are the files discussed in this milestone.
-
-
-
-
-
+
-
+
-
+
@@ -1282,18 +1259,18 @@ The **Routing Module** has several characteristics:
### Refactor the routing configuration into a _routing module_
-Create a file named `app-routing.module.ts` in the `/app` folder to contain the routing module.
+Create an `AppRouting` module in the `/app` folder to contain the routing configuration.
+
+
+ ng generate module app-routing --module app --flat
+
-Import the `CrisisListComponent` and the `HeroListComponent` components
+Import the `CrisisListComponent`, `HeroListComponent`, and `PageNotFoundCompponent` symbols
just like you did in the `app.module.ts`. Then move the `Router` imports
and routing configuration, including `RouterModule.forRoot`, into this routing module.
-Following convention, add a class name `AppRoutingModule` and export it
-so you can import it later in `AppModule`.
-
-Finally, re-export the Angular `RouterModule` by adding it to the module `exports` array.
-By re-exporting the `RouterModule` here and importing `AppRoutingModule` in `AppModule`,
-the components declared in `AppModule` will have access to router directives such as `RouterLink` and `RouterOutlet`.
+Re-export the Angular `RouterModule` by adding it to the module `exports` array.
+By re-exporting the `RouterModule` here the components declared in `AppModule` will have access to router directives such as `RouterLink` and `RouterOutlet`.
After these steps, the file should look like this.
@@ -1301,11 +1278,8 @@ After these steps, the file should look like this.
-
-
-Next, update the `app.module.ts` file,
-first importing the newly created `AppRoutingModule`from `app-routing.module.ts`,
-then replacing `RouterModule.forRoot` in the `imports` array with the `AppRoutingModule`.
+Next, update the `app.module.ts` file, removing `RouterModule.forRoot` in
+the `imports` array.
@@ -1317,7 +1291,7 @@ then replacing `RouterModule.forRoot` in the `imports` array with the `AppRoutin
-Later in this guide you will create [multiple routing modules](#hero-routing-module) and discover that
+Later in this guide you will create [multiple routing modules](#heroes-functionality) and discover that
you must import those routing modules [in the correct order](#routing-module-order).
@@ -1395,24 +1369,40 @@ Then you'll import into the main module and navigate among them.
Follow these steps:
-* Create the `src/app/heroes` folder; you'll be adding files implementing *hero management* there.
-* Delete the placeholder `hero-list.component.ts` that's in the `app` folder.
-* Create a new `hero-list.component.ts` under `src/app/heroes`.
-* Copy into it the contents of the `app.component.ts` from
- the "Services" tutorial.
-* Make a few minor but necessary changes:
+* Create a `HeroesModule` with routing in the heroes folder and register it with the root `AppModule`. This is where you'll be implementing the *hero management*.
+
+
+ ng generate module heroes/heroes --module app --flat --routing
+
+
+* Move the placeholder `hero-list` folder that's in the `app` into the `heroes` folder.
+* Copy the contents of the `heroes/heroes.component.html` from
+ the "Services" tutorial into the `hero-list.component.html` template.
- * Delete the `selector` (routed components don't need them).
- * Delete the `
`.
* Relabel the `
` to `
HEROES
`.
- * Delete the `` at the bottom of the template.
- * Rename the `AppComponent` class to `HeroListComponent`.
+ * Delete the `` component at the bottom of the template.
-* Copy the `hero-detail.component.ts` and the `hero.service.ts` files into the `heroes` subfolder.
-* Create a (pre-routing) `heroes.module.ts` in the heroes folder that looks like this:
+* Copy the contents of the `heroes/heroes.component.css` from the live example into the `hero-list.component.css` file.
+* Copy the contents of the `heroes/heroes.component.ts` from the live example into the `hero-list.component.ts` file.
+ * Change the component class name to `HeroListComponent`.
+ * Change the `selector` to `app-hero-list`.
+
+
-
+ Selectors are **not required** for _routed components_ due to the components are dynamically inserted when the page is rendered, but are useful for identifying and targeting them in your HTML element tree.
+
+
+
+* Copy the `hero-detail` folder, the `hero.ts`, `hero.service.ts`, and `mock-heroes.ts` files into the `heroes` subfolder.
+* Copy the `message.service.ts` into the `src/app` folder.
+* Update the relative path import to the `message.service` in the `hero.service.ts` file.
+
+Next, you'll update the `HeroesModule` metadata.
+
+ * Import and add the `HeroDetailComponent` and `HeroListComponent` to the `declarations` array in the `HeroesModule`.
+
+
@@ -1430,21 +1420,70 @@ When you're done, you'll have these *hero management* files:
- hero-detail.component.ts
+ hero-detail
+
+
+
+ hero-detail.component.css
+
+
+
+ hero-detail.component.html
+
+
+
+ hero-detail.component.ts
+
+
+
+
- hero-list.component.ts
+ hero-list
+
+
+
+ hero-list.component.css
+
+
+
+ hero-list.component.html
+
+
+
+ hero-list.component.ts
+
+
+
+
hero.service.ts
+
+
+
+ hero.ts
+
+ heroes-routing.module.ts
+
+
heroes.module.ts
+
+ mock-heroes.ts
+
+
+
+
+
+
+
@@ -1454,7 +1493,7 @@ When you're done, you'll have these *hero management* files:
{@a hero-routing-requirements}
-### *Hero* feature routing requirements
+#### *Hero* feature routing requirements
The heroes feature has two interacting components, the hero list and the hero detail.
The list view is self-sufficient; you navigate to it, it gets a list of heroes and displays them.
@@ -1466,40 +1505,7 @@ When the user selects a hero from the list, the app should navigate to the detai
and show that hero.
You tell the detail view which hero to display by including the selected hero's id in the route URL.
-
-{@a hero-routing-module}
-
-
-### *Hero* feature route configuration
-
-Create a new `heroes-routing.module.ts` in the `heroes` folder
-using the same techniques you learned while creating the `AppRoutingModule`.
-
-
-
-
-
-
-
-
-
-
-
-
-Put the routing module file in the same folder as its companion module file.
-Here both `heroes-routing.module.ts` and `heroes.module.ts` are in the same `src/app/heroes` folder.
-
-Consider giving each feature module its own route configuration file.
-It may seem like overkill early when the feature routes are simple.
-But routes have a tendency to grow more complex and consistency in patterns pays off over time.
-
-
-
-
-
-
-Import the hero components from their new locations in the `src/app/heroes/` folder, define the two hero routes,
-and export the `HeroRoutingModule` class.
+Import the hero components from their new locations in the `src/app/heroes/` folder, define the two hero routes.
Now that you have routes for the `Heroes` module, register them with the `Router` via the
`RouterModule` _almost_ as you did in the `AppRoutingModule`.
@@ -1519,31 +1525,30 @@ In any other module, you must call the **`RouterModule.forChild`** method to reg
+The updated `HeroesRoutingModule` looks like this:
-{@a adding-routing-module}
+
+
-### Add the routing module to the _HeroesModule_
-Add the `HeroRoutingModule` to the `HeroModule`
-just as you added `AppRoutingModule` to the `AppModule`.
-Open `heroes.module.ts`.
-Import the `HeroRoutingModule` token from `heroes-routing.module.ts` and
-add it to the `imports` array of the `HeroesModule`.
-The finished `HeroesModule` looks like this:
+
-
-
+Consider giving each feature module its own route configuration file.
+It may seem like overkill early when the feature routes are simple.
+But routes have a tendency to grow more complex and consistency in patterns pays off over time.
+
+
{@a remove-duplicate-hero-routes}
-### Remove duplicate hero routes
+#### Remove duplicate hero routes
The hero routes are currently defined in _two_ places: in the `HeroesRoutingModule`,
by way of the `HeroesModule`, and in the `AppRoutingModule`.
@@ -1551,7 +1556,6 @@ by way of the `HeroesModule`, and in the `AppRoutingModule`.
Routes provided by feature modules are combined together into their imported module's routes by the router.
This allows you to continue defining the feature module routes without modifying the main route configuration.
-But you don't want to define the same routes twice.
Remove the `HeroListComponent` import and the `/heroes` route from the `app-routing.module.ts`.
**Leave the default and the wildcard routes!**
@@ -1567,20 +1571,9 @@ These are concerns at the top level of the application itself.
{@a merge-hero-routes}
-### Import hero module into AppModule
-The heroes feature module is ready, but the application doesn't know about the `HeroesModule` yet.
-Open `app.module.ts` and revise it as follows.
-
-Import the `HeroesModule` and add it to the `imports` array in the `@NgModule` metadata of the `AppModule`.
+#### Remove heroes declarations
-Remove the `HeroListComponent` from the `AppModule`'s `declarations` because it's now provided by the `HeroesModule`.
-This is important. There can be only _one_ owner for a declared component.
-In this case, the `Heroes` module is the owner of the `Heroes` components and is making them available to
-components in the `AppModule` via the `HeroesModule`.
-
-As a result, the `AppModule` no longer has specific knowledge of the hero feature, its components, or its route details.
-You can evolve the hero feature with more components and different routes.
-That's a key benefit of creating a separate module for each feature area.
+Remove the `HeroListComponent` from the `AppModule`'s `declarations` because it's now provided by the `HeroesModule`. You can evolve the hero feature with more components and different routes. That's a key benefit of creating a separate module for each feature area.
After these steps, the `AppModule` should look like this:
@@ -1634,12 +1627,12 @@ Learn about inspecting the runtime router configuration
-
+### Route Parameters
{@a route-def-with-parameter}
-### Route definition with a parameter
+#### Route definition with a parameter
Return to the `HeroesRoutingModule` and look at the route definitions again.
The route to `HeroDetailComponent` has a twist.
@@ -1659,7 +1652,7 @@ you expect a hero id to appear in the browser URL like this:
- localhost:3000/hero/15
+ localhost:4200/hero/15
@@ -1692,7 +1685,7 @@ a route for some other hero.
{@a route-parameters}
-### Setting the route parameters in the list view
+#### Setting the route parameters in the list view
After navigating to the `HeroDetailComponent`, you expect to see the details of the selected hero.
You need *two* pieces of information: the routing path to the component and the hero's `id`.
@@ -1701,14 +1694,14 @@ Accordingly, the _link parameters array_ has *two* items: the routing _path_ an
`id` of the selected hero.
-
+
The router composes the destination URL from the array like this:
-`localhost:3000/hero/15`.
+`localhost:4200/hero/15`.
@@ -1732,7 +1725,7 @@ the `HeroDetailComponent` via the `ActivatedRoute` service.
Import the `Router`, `ActivatedRoute`, and `ParamMap` tokens from the router package.
-
+
@@ -1741,7 +1734,7 @@ Import the `Router`, `ActivatedRoute`, and `ParamMap` tokens from the router pac
Import the `switchMap` operator because you need it later to process the `Observable` route parameters.
-
+
@@ -1754,7 +1747,7 @@ As usual, you write a constructor that asks Angular to inject services
that the component requires and reference them as private variables.
-
+
@@ -1762,7 +1755,7 @@ Later, in the `ngOnInit` method, you use the `ActivatedRoute` service to retriev
pull the hero `id` from the parameters and retrieve the hero to display.
-
+
@@ -1901,7 +1894,7 @@ You can access the parameters directly without subscribing or adding observable
It's much simpler to write and read:
-
+
@@ -1934,28 +1927,28 @@ that you can bind to a `[routerLink]` directive.
It holds the _path to the `HeroListComponent`_:
-
+
{@a optional-route-parameters}
-### Route Parameters: Required or optional?
+#### Route Parameters: Required or optional?
Use [*route parameters*](#route-parameters) to specify a *required* parameter value *within* the route URL
as you do when navigating to the `HeroDetailComponent` in order to view the hero with *id* 15:
- localhost:3000/hero/15
+ localhost:4200/hero/15
You can also add *optional* information to a route request.
-For example, when returning to the heroes list from the hero detail view,
+For example, when returning to the hero-detail.component.ts list from the hero detail view,
it would be nice if the viewed hero was preselected in the list.
@@ -1990,13 +1983,13 @@ prefer an *optional parameter* when the value is optional, complex, and/or multi
{@a optionally-selecting}
-### Heroes list: optionally selecting a hero
+#### Heroes list: optionally selecting a hero
When navigating to the `HeroDetailComponent` you specified the _required_ `id` of the hero-to-edit in the
*route parameter* and made it the second item of the [_link parameters array_](#link-parameters-array).
-
+
@@ -2016,7 +2009,7 @@ When the user clicks the back button, the `HeroDetailComponent` constructs anoth
which it uses to navigate back to the `HeroListComponent`.
-
+
@@ -2033,7 +2026,7 @@ For demonstration purposes, there's an extra junk parameter (`foo`) in the objec
Here's the revised navigation statement:
-
+
@@ -2048,7 +2041,7 @@ It should look something like this, depending on where you run it:
- localhost:3000/heroes;id=15;foo=foo
+ localhost:4200/heroes;id=15;foo=foo
@@ -2115,7 +2108,7 @@ This time you'll be navigating in the opposite direction, from the `HeroDetailCo
First you extend the router import statement to include the `ActivatedRoute` service symbol:
-
+
@@ -2124,7 +2117,7 @@ First you extend the router import statement to include the `ActivatedRoute` ser
Import the `switchMap` operator to perform an operation on the `Observable` of route parameter map.
-
+
@@ -2133,7 +2126,7 @@ Import the `switchMap` operator to perform an operation on the `Observable` of r
Then you inject the `ActivatedRoute` in the `HeroListComponent` constructor.
-
+
@@ -2148,7 +2141,13 @@ The binding adds the `selected` CSS class when the comparison returns `true` and
Look for it within the repeated `
` tag as shown here:
-
+
+
+
+
+Add some styles to apply when the list item is selected.
+
+
@@ -2164,16 +2163,17 @@ When the user navigates from the heroes list to the "Magneta" hero and back, "Ma
The optional `foo` route parameter is harmless and continues to be ignored.
+### Adding routable animations
{@a route-animation}
-### Adding animations to the routed component
+#### Adding animations to the routed component
The heroes feature module is almost complete, but what is a feature without some smooth transitions?
This section shows you how to add some [animations](guide/animations) to the `HeroDetailComponent`.
-First import `BrowserAnimationsModule`:
+First import the `BrowserAnimationsModule` and add it to the `imports` array:
@@ -2203,9 +2203,12 @@ This file does the following:
You could also create more transitions for other routes. This trigger is sufficient for the current milestone.
-Back in the `AppComponent`, import the `RouterOutlet` token from the `@angular/router` package and the `slideInDownAnimation` from `'./animations.ts`.
+Back in the `AppComponent`, import the `RouterOutlet` token from the `@angular/router` package and the `slideInDownAnimation` from
+`'./animations.ts`.
+
+Add an `animations` array to the `@Component` metadata's that contains the `slideInDownAnimation`.
-
+
@@ -2214,9 +2217,7 @@ use the `@routeAnimation` trigger and bind it to the element.
For the `@routeAnimation` transitions to key off states, you'll need to provide it with the `data` from the `ActivatedRoute`. The `RouterOutlet` is exposed as an `outlet` template variable, so you bind a reference to the router outlet. A variable of `routerOutlet` is an ideal choice.
-Add an `animations` array to the `@Component` metadata's that contains the `slideInDownAnimation`.
-
-
+
@@ -2249,7 +2250,7 @@ After these changes, the folder structure looks like this:
- router-sample
+ angular-router-sample
@@ -2264,58 +2265,156 @@ After these changes, the folder structure looks like this:
app
@@ -2346,6 +2445,14 @@ Here are the relevant files for this version of the sample application.
+
+
+
+
+
+
+
+
@@ -2354,15 +2461,27 @@ Here are the relevant files for this version of the sample application.
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
@@ -2378,6 +2497,10 @@ Here are the relevant files for this version of the sample application.
+
+
+
+
@@ -2392,20 +2515,19 @@ It's time to add real features to the app's current placeholder crisis center.
Begin by imitating the heroes feature:
-* Delete the placeholder crisis center file.
-* Create an `app/crisis-center` folder.
-* Copy the files from `app/heroes` into the new crisis center folder.
+* Create a `crisis-center` subfolder in the `src/app` folder.
+* Copy the files and folders from `app/heroes` into the new `crisis-center` folder.
* In the new files, change every mention of "hero" to "crisis", and "heroes" to "crises".
+* Rename the NgModule files to `crisis-center.module.ts` and `crisis-center-routing.module.ts`.
-You'll turn the `CrisisService` into a purveyor of mock crises instead of mock heroes:
+You'll use mock crises instead of mock heroes:
-
+
-
The resulting crisis center is a foundation for introducing a new concept—**child routing**.
You can leave *Heroes* in its current state as a contrast with the *Crisis Center*
and decide later if the differences are worthwhile.
@@ -2452,9 +2574,15 @@ If your app had many feature areas, the app component trees might look like this
### Child routing component
-Add the following `crisis-center.component.ts` to the `crisis-center` folder:
+Generate a `CrisisCenter` component in the `crisis-center` folder:
+
+
+ ng generate component crisis-center/crisis-center
+
+
+Update the component template to look like this:
-
+
The `CrisisCenterComponent` has the following in common with the `AppComponent`:
@@ -2466,11 +2594,7 @@ just as the `AppComponent` is a shell to manage the high-level workflow.
Like most shells, the `CrisisCenterComponent` class is very simple, simpler even than `AppComponent`:
it has no business logic, and its template has no links, just a title and
-`` for the crisis center child views.
-
-Unlike `AppComponent`, and most other components, it _lacks a selector_.
-It doesn't _need_ one since you don't *embed* this component in a parent template,
-instead you use the router to *navigate* to it.
+`` for the crisis center child component.
{@a child-route-config}
@@ -2478,14 +2602,19 @@ instead you use the router to *navigate* to it.
### Child route configuration
-As a host page for the "Crisis Center" feature, add the following `crisis-center-home.component.ts` to the `crisis-center` folder.
+As a host page for the "Crisis Center" feature, generate a `CrisisCenterHome` component in the `crisis-center` folder.
-
+
+ ng generate component crisis-center/crisis-center-home
-Create a `crisis-center-routing.module.ts` file as you did the `heroes-routing.module.ts` file.
-This time, you define **child routes** *within* the parent `crisis-center` route.
+Update the template with a welcome message to the `Crisis Center`.
+
+
+
+Update the `crisis-center-routing.module.ts` you renamed after copying it from `heroes-routing.module.ts` file.
+This time, you define **child routes** *within* the parent `crisis-center` route.
@@ -2506,8 +2635,8 @@ of the `CrisisCenterComponent`, not in the `RouterOutlet` of the `AppComponent`
The `CrisisListComponent` contains the crisis list and a `RouterOutlet` to
display the `Crisis Center Home` and `Crisis Detail` route components.
-The `Crisis Detail` route is a child of the `Crisis List`. Since the router [reuses components](#reuse)
-by default, the `Crisis Detail` component will be re-used as you select different crises.
+The `Crisis Detail` route is a child of the `Crisis List`. The router [reuses components](#reuse)
+by default, so the `Crisis Detail` component will be re-used as you select different crises.
In contrast, back in the `Hero Detail` route, the component was recreated each time you selected a different hero.
At the top level, paths that begin with `/` refer to the root of the application.
@@ -2525,7 +2654,7 @@ Apply that logic to navigation within the crisis center for which the parent pat
The absolute URL for the latter example, including the `localhost` origin, is
- localhost:3000/crisis-center/2
+ localhost:4200/crisis-center/2
@@ -2548,12 +2677,17 @@ Here's the complete `crisis-center-routing.module.ts` file with its imports.
As with the `HeroesModule`, you must add the `CrisisCenterModule` to the `imports` array of the `AppModule`
_before_ the `AppRoutingModule`:
+
+
+
-
+
-
+
+
+
Remove the initial crisis center route from the `app-routing.module.ts`.
The feature routes are now provided by the `HeroesModule` and the `CrisisCenter` modules.
@@ -2640,7 +2774,7 @@ The `ActivatedRoute` is implicit in a `RouterLink` directive.
Update the `gotoCrises` method of the `CrisisDetailComponent` to navigate back to the *Crisis Center* list using relative path navigation.
-
+
@@ -2673,7 +2807,7 @@ Multiple outlets can be displaying different content, determined by different ro
Add an outlet named "popup" in the `AppComponent`, directly below the unnamed outlet.
-
+
@@ -2696,7 +2830,12 @@ They differ in a few key respects.
* They work in combination with other routes.
* They are displayed in named outlets.
-Create a new component named `ComposeMessageComponent` in `src/app/compose-message.component.ts`.
+Generate a new component to compose the message.
+
+
+ ng generate component compose-message
+
+
It displays a simple form with a header, an input box for the message,
and two buttons, "Send" and "Cancel".
@@ -2707,16 +2846,20 @@ and two buttons, "Send" and "Cancel".
-Here's the component and its template:
+Here's the component, its template and styles:
-
+
+
+
+
+
-
+
@@ -2732,9 +2875,6 @@ Note that the `send()` method simulates latency by waiting a second before "send
The `closePopup()` method closes the popup view by navigating to the popup outlet with a `null`.
That's a peculiarity covered [below](#clear-secondary-routes).
-As with other application components, you add the `ComposeMessageComponent` to the `declarations` of an `NgModule`.
-Do so in the `AppModule`.
-
{@a add-secondary-route}
@@ -2756,7 +2896,7 @@ This route now targets the popup outlet and the `ComposeMessageComponent` will d
The user needs a way to open the popup.
Open the `AppComponent` and add a "Contact" link.
-
+
@@ -2851,7 +2991,7 @@ That's why the popup stays visible as you navigate among the crises and heroes.
Clicking the "send" or "cancel" buttons _does_ clear the popup view.
To see how, look at the `closePopup()` method again:
-
+
@@ -2948,7 +3088,29 @@ In this next section, you'll extend the crisis center with some new *administrat
Those features aren't defined yet.
But you can start by adding a new feature module named `AdminModule`.
-Create an `admin` folder with a feature module file, a routing configuration file, and supporting components.
+Generate an `admin` folder with a feature module file and a routing configuration file.
+
+
+ ng generate module admin --routing
+
+
+Next, generate the supporting components.
+
+
+ ng generate component admin/admin-dashboard
+
+
+
+ ng generate component admin/admin
+
+
+
+ ng generate component admin/manage-crises
+
+
+
+ ng generate component admin/manage-heroes
+
The admin feature file structure looks like this:
@@ -2962,27 +3124,91 @@ The admin feature file structure looks like this:
@@ -2997,11 +3223,11 @@ feature module, a dashboard route and two unfinished components to manage crises
-
+
-
+
@@ -3009,11 +3235,11 @@ feature module, a dashboard route and two unfinished components to manage crises
-
+
-
+
@@ -3025,7 +3251,7 @@ feature module, a dashboard route and two unfinished components to manage crises
-Since the admin dashboard `RouterLink` is an empty path route in the `AdminComponent`, it
+Although the admin dashboard `RouterLink` is an empty path route in the `AdminComponent`, it
is considered a match to any route within the admin feature area.
You only want the `Dashboard` link to be active when the user visits that route.
Adding an additional binding to the `Dashboard` routerLink,
@@ -3036,6 +3262,10 @@ the user navigates to the `/admin` URL and not when navigating to any of the chi
+{@a component-less-route}
+
+
+##### Component-less route: grouping routes without a component
The initial admin routing configuration:
@@ -3044,12 +3274,6 @@ The initial admin routing configuration:
-
-
-{@a component-less-route}
-
-
-### Component-less route: grouping routes without a component
Looking at the child route under the `AdminComponent`, there is a `path` and a `children`
property but it's not using a `component`.
You haven't made a mistake in the configuration.
@@ -3073,7 +3297,7 @@ to register the admin routes.
Add an "Admin" link to the `AppComponent` shell so that users can get to this feature.
-
+
@@ -3093,14 +3317,18 @@ Instead you'll write a `canActivate()` guard method to redirect anonymous users
login page when they try to enter the admin area.
This is a general purpose guard—you can imagine other features
-that require authenticated users—so you create an
-`auth-guard.service.ts` in the application root folder.
+that require authenticated users—so you generate an
+`AuthGuard` in the `auth` folder.
+
+
+ ng generate guard auth/auth
+
At the moment you're interested in seeing how guards work so the first version does nothing useful.
It simply logs to console and `returns` true immediately, allowing navigation to proceed:
-
+
@@ -3115,7 +3343,7 @@ update the admin route with a `canActivate` guard property that references it:
-
+
The admin feature is now protected by the guard, albeit protected poorly.
@@ -3126,11 +3354,15 @@ The admin feature is now protected by the guard, albeit protected poorly.
Make the `AuthGuard` at least pretend to authenticate.
-The `AuthGuard` should call an application service that can login a user and retain information about the current user.
-Here's a demo `AuthService`:
+The `AuthGuard` should call an application service that can login a user and retain information about the current user. Generate a new `AuthService` in the `admin` folder:
+
+
+ ng generate service admin/auth
+
+Update the `AuthService` to log in the user:
-
+
@@ -3145,7 +3377,7 @@ The `redirectUrl` property will store the attempted URL so you can navigate to i
Revise the `AuthGuard` to call it.
-
+
@@ -3175,40 +3407,32 @@ You need a `LoginComponent` for the user to log in to the app. After logging in,
to the stored URL if available, or use the default URL.
There is nothing new about this component or the way you wire it into the router configuration.
-Register a `/login` route in the `login-routing.module.ts` and add the necessary providers to the `providers`
-array. In `app.module.ts`, import the `LoginComponent` and add it to the `AppModule` `declarations`.
-Import and add the `LoginRoutingModule` to the `AppModule` imports as well.
+
+ ng generate component auth/login
+
+
+Register a `/login` route in the `auth/auth-routing.module.ts`. In `app.module.ts`, import and add the `AuthModule` to the `AppModule` imports.
-
+
-
+
-
+
-
+
-
-
-
-
-
-
-
-
-Guards and the service providers they require _must_ be provided at the module-level. This allows
-the Router access to retrieve these services from the `Injector` during the navigation process.
-The same rule applies for feature modules loaded [asynchronously](#asynchronous-routing).
+
+
-
-
+
{@a can-activate-child-guard}
@@ -3224,7 +3448,7 @@ You protected the admin feature module from unauthorized access.
You should also protect child routes _within_ the feature module.
Extend the `AuthGuard` to protect when navigating between the `admin` routes.
-Open `auth-guard.service.ts` and add the `CanActivateChild` interface to the imported tokens from the router package.
+Open `auth.guard.ts` and add the `CanActivateChild` interface to the imported tokens from the router package.
Next, implement the `canActivateChild()` method which takes the same arguments as the `canActivate()` method:
an `ActivatedRouteSnapshot` and `RouterStateSnapshot`.
@@ -3233,7 +3457,7 @@ async checks and a `boolean` for sync checks.
This one returns a `boolean`:
-
+
@@ -3281,7 +3505,7 @@ You need the `CanDeactivate` guard.
{@a cancel-save}
-### Cancel and save
+#### Cancel and save
The sample application doesn't talk to a server.
Fortunately, you have another way to demonstrate an asynchronous router hook.
@@ -3294,7 +3518,7 @@ discards the changes when the user presses the *Cancel* button.
Both buttons navigate back to the crisis list after save or cancel.
-
+
@@ -3322,7 +3546,17 @@ is like waiting for the server asynchronously.
-The `DialogService`, provided in the `AppModule` for app-wide use, does the asking.
+Generate a `Dialog` service to handle user confirmation.
+
+
+ ng generate service dialog
+
+
+Add a `confirm()` method to the `DialogService` to prompt the user to confirm their intent. The `window.confirm` is a _blocking_ action that displays a modal dialog and waits for user interaction.
+
+
+
+
It returns an `Observable` that *resolves* when the user eventually decides what to do: either
to discard changes and navigate away (`true`) or to preserve the pending changes and stay in the crisis editor (`false`).
@@ -3331,7 +3565,12 @@ to discard changes and navigate away (`true`) or to preserve the pending changes
{@a CanDeactivate}
-Create a _guard_ that checks for the presence of a `canDeactivate()` method in a component—any component.
+Generate a _guard_ that checks for the presence of a `canDeactivate()` method in a component—any component.
+
+
+ ng generate guard can-deactivate
+
+
The `CrisisDetailComponent` will have this method.
But the guard doesn't have to know that.
The guard shouldn't know the details of any component's deactivation method.
@@ -3339,7 +3578,7 @@ It need only detect that the component has a `canDeactivate()` method and call i
This approach makes the guard reusable.
-
+
@@ -3354,7 +3593,7 @@ wanted to use this guard for this component and needed to get
the component's properties or confirm whether the router should allow navigation away from it.
-
+
@@ -3363,7 +3602,7 @@ the component's properties or confirm whether the router should allow navigation
Looking back at the `CrisisDetailComponent`, it implements the confirmation workflow for unsaved changes.
-
+
@@ -3383,17 +3622,6 @@ Add the `Guard` to the crisis detail route in `crisis-center-routing.module.ts`
-
-Add the `Guard` to the main `AppRoutingModule` `providers` array so the
-`Router` can inject it during the navigation process.
-
-
-
-
-
-
-
-
Now you have given the user a safeguard against unsaved changes.
{@a Resolve}
@@ -3421,7 +3649,7 @@ You need a *resolver*.
{@a fetch-before-navigating}
-### Fetch data before navigating
+#### Fetch data before navigating
At the moment, the `CrisisDetailComponent` retrieves the selected crisis.
If the crisis is not found, it navigates back to the crisis list view.
@@ -3430,17 +3658,21 @@ The experience might be better if all of this were handled first, before the rou
A `CrisisDetailResolver` service could retrieve a `Crisis` or navigate away if the `Crisis` does not exist
_before_ activating the route and creating the `CrisisDetailComponent`.
-Create the `crisis-detail-resolver.service.ts` file within the `Crisis Center` feature area.
+Generate a `CrisisDetailResolver` service file within the `Crisis Center` feature area.
+
+
+ ng generate service crisis-center/crisis-detail-resolver
+
-
+
Take the relevant parts of the crisis retrieval logic in `CrisisDetailComponent.ngOnInit`
-and move them into the `CrisisDetailResolver`.
+and move them into the `CrisisDetailResolverService`.
Import the `Crisis` model, `CrisisService`, and the `Router`
so you can navigate elsewhere if you can't fetch the crisis.
@@ -3452,17 +3684,20 @@ That method could return a `Promise`, an `Observable`, or a synchronous return v
The `CrisisService.getCrisis` method returns an observable, in order to prevent the route from loading until the data is fetched.
The `Router` guards require an observable to `complete`, meaning it has emitted all
of its values. You use the `take` operator with an argument of `1` to ensure that the
-observable completes after retrieving the first value from the observable returned by the
-`getCrisis` method. If it doesn't return a valid `Crisis`, navigate the user back to the `CrisisListComponent`,
-canceling the previous in-flight navigation to the `CrisisDetailComponent`.
+Observable completes after retrieving the first value from the Observable returned by the
+`getCrisis` method.
+
+If it doesn't return a valid `Crisis`, return an empty `Observable`, canceling the previous in-flight navigation to the `CrisisDetailComponent` and navigate the user back to the `CrisisListComponent`. The update resolver service looks like this:
+
+
+
+
Import this resolver in the `crisis-center-routing.module.ts`
and add a `resolve` object to the `CrisisDetailComponent` route configuration.
-Remember to add the `CrisisDetailResolver` service to the `CrisisCenterRoutingModule`'s `providers` array.
-
-
+
@@ -3474,7 +3709,7 @@ that's where you said it should be when you re-configured the route.
It will be there when the `CrisisDetailComponent` ask for it.
-
+
@@ -3483,7 +3718,7 @@ It will be there when the `CrisisDetailComponent` ask for it.
**Three critical points**
1. The router's `Resolve` interface is optional.
-The `CrisisDetailResolver` doesn't inherit from a base class.
+The `CrisisDetailResolverService` doesn't inherit from a base class.
The router looks for that method and calls it if found.
1. Rely on the router to call the resolver.
@@ -3498,15 +3733,15 @@ The relevant *Crisis Center* code for this milestone follows.
-
+
-
+
-
+
@@ -3514,11 +3749,19 @@ The relevant *Crisis Center* code for this milestone follows.
-
+
+
+
+
+
+
+
+
+
-
+
@@ -3530,17 +3773,21 @@ The relevant *Crisis Center* code for this milestone follows.
-
+
+
+
+
+Guards
-
+
-
+
@@ -3570,7 +3817,7 @@ Add an `anchor` element so you can jump to a certain point on the page.
Add the `NavigationExtras` object to the `router.navigate` method that navigates you to the `/login` route.
-
+
@@ -3583,7 +3830,7 @@ and provide the `queryParamsHandling` and `preserveFragment` to pass along the c
and fragment to the next route.
-
+
@@ -3598,11 +3845,11 @@ when navigating.
-Since you'll be navigating to the *Admin Dashboard* route after logging in, you'll update it to handle the
+As you'll be navigating to the *Admin Dashboard* route after logging in, you'll update it to handle the
query parameters and fragment.
-
+
@@ -3671,16 +3918,20 @@ Users will still visit `/admin` and the `AdminComponent` still serves as the *Ro
Open the `AppRoutingModule` and add a new `admin` route to its `appRoutes` array.
-Give it a `loadChildren` property (not a `children` property!), set to the address of the `AdminModule`.
+Give it a `loadChildren` property instead of a `children` property, set to the address of the `AdminModule`.
The address is the `AdminModule` file location (relative to the app root),
-followed by a `#` separator,
-followed by the name of the exported module class, `AdminModule`.
+followed by a `#` separator, followed by the name of the exported module class, `AdminModule`.
+
+
+*Note*: When using absolute paths, the `NgModule` file location must begin with `src/app` in order to resolve correctly. For custom [path mapping with absolute paths](https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping), the `baseUrl` and `paths` properties in the project `tsconfig.json` must be configured.
+
+
When the router navigates to this route, it uses the `loadChildren` string to dynamically load the `AdminModule`.
@@ -3727,13 +3978,13 @@ Add a **`CanLoad`** guard that only loads the `AdminModule` once the user is log
The existing `AuthGuard` already has the essential logic in
its `checkLogin()` method to support the `CanLoad` guard.
-Open `auth-guard.service.ts`.
+Open `auth.guard.ts`.
Import the `CanLoad` interface from `@angular/router`.
Add it to the `AuthGuard` class's `implements` list.
Then implement `canLoad()` as follows:
-
+
@@ -3818,12 +4069,13 @@ Take the same steps you used to configure `AdminModule` for lazy load.
1. Remove all mention of the `CrisisCenterModule` from `app.module.ts`.
+
Here are the updated modules _before enabling preload_:
-
+
@@ -3855,7 +4107,7 @@ Add the `PreloadAllModules` token to the `forRoot` call:
This tells the `Router` preloader to immediately load _all_ lazy loaded routes (routes with a `loadChildren` property).
-When you visit `http://localhost:3000`, the `/heroes` route loads immediately upon launch
+When you visit `http://localhost:4200`, the `/heroes` route loads immediately upon launch
and the router starts loading the `CrisisCenterModule` right after the `HeroesModule` loads.
Surprisingly, the `AdminModule` does _not_ preload. Something is blocking it.
@@ -3898,18 +4150,20 @@ Set the `data.preload` flag in the `crisis-center` route in the `AppRoutingModul
+Generate a new `SelectivePreloadingStrategy` service.
+
+ ng generate service selective-preloading-strategy
+
-Add a new file to the project called `selective-preloading-strategy.ts`
-and define a `SelectivePreloadingStrategy` service class as follows:
-
+
-`SelectivePreloadingStrategy` implements the `PreloadingStrategy`, which has one method, `preload`.
+`SelectivePreloadingStrategyService` implements the `PreloadingStrategy`, which has one method, `preload`.
The router calls the `preload` method with two arguments:
@@ -3923,27 +4177,27 @@ If the route should _not_ preload, it returns an `Observable` of `null`.
In this sample, the `preload` method loads the route if the route's `data.preload` flag is truthy.
It also has a side-effect.
-`SelectivePreloadingStrategy` logs the `path` of a selected route in its public `preloadedModules` array.
+`SelectivePreloadingStrategyService` logs the `path` of a selected route in its public `preloadedModules` array.
Shortly, you'll extend the `AdminDashboardComponent` to inject this service and display its `preloadedModules` array.
But first, make a few changes to the `AppRoutingModule`.
-1. Import `SelectivePreloadingStrategy` into `AppRoutingModule`.
-1. Replace the `PreloadAllModules` strategy in the call to `forRoot` with this `SelectivePreloadingStrategy`.
-1. Add the `SelectivePreloadingStrategy` strategy to the `AppRoutingModule` providers array so it can be injected
+1. Import `SelectivePreloadingStrategyService` into `AppRoutingModule`.
+1. Replace the `PreloadAllModules` strategy in the call to `forRoot` with this `SelectivePreloadingStrategyService`.
+1. Add the `SelectivePreloadingStrategyService` strategy to the `AppRoutingModule` providers array so it can be injected
elsewhere in the app.
Now edit the `AdminDashboardComponent` to display the log of preloaded routes.
-1. Import the `SelectivePreloadingStrategy` (it's a service).
+1. Import the `SelectivePreloadingStrategyService`.
1. Inject it into the dashboard's constructor.
1. Update the template to display the strategy service's `preloadedModules` array.
When you're done it looks like this.
-
+
@@ -3991,9 +4245,15 @@ So instead, you'll update the empty path route in `app-routing.module.ts` to red
-Since `RouterLink`s aren't tied to route configuration, you'll need to update the associated router links so they remain active when the new route is active. You'll update the `app.component.ts` template for the `/heroes` routerLink.
+`RouterLink`s aren't tied to route configuration, so you'll need to update the associated router links so they remain active when the new route is active. You'll update the `app.component.ts` template for the `/heroes` routerLink.
+
+
+
+
+
+Update the `goToHeroes()` method in the `hero-detail.component.ts` to navigate back to `/superheroes` with the optional route parameters.
-
+
@@ -4016,7 +4276,7 @@ examining its `config` property.
For example, update the `AppModule` as follows and look in the browser console window
to see the finished route configuration.
-
+
@@ -4066,7 +4326,7 @@ You can bind the `RouterLink` directive to such an array like this:
You've written a two element array when specifying a route parameter like this:
-
+
@@ -4220,6 +4480,47 @@ without hashes (#) in the middle.
Stick with the default unless you have a compelling reason to
resort to hash routes.
+
+#### The *<base href>*
+
+The router uses the browser's
+history.pushState
+for navigation. Thanks to `pushState`, you can make in-app URL paths look the way you want them to
+look, e.g. `localhost:4200/crisis-center`. The in-app URLs can be indistinguishable from server URLs.
+
+Modern HTML5 browsers were the first to support `pushState` which is why many people refer to these URLs as
+"HTML5 style" URLs.
+
+
+
+
+
+
+HTML5 style navigation is the router default.
+In the [LocationStrategy and browser URL styles](#browser-url-styles) Appendix,
+learn why HTML5 style is preferred, how to adjust its behavior, and how to switch to the
+older hash (#) style, if necessary.
+
+
+
+
+
+
+You must **add a
+<base href> element**
+to the app's `index.html` for `pushState` routing to work.
+The browser uses the `` value to prefix *relative* URLs when referencing
+CSS files, scripts, and images.
+
+Add the `` element just after the `` tag.
+If the `app` folder is the application root, as it is for this application,
+set the `href` value in **`index.html`** *exactly* as shown here.
+
+
+
+
+
+
#### HTML5 URLs and the *<base href>*
While the router uses the
From 816f0017e7485670b3c379db32ab344dd5990847 Mon Sep 17 00:00:00 2001
From: Brandon Roberts
Date: Mon, 10 Sep 2018 08:37:21 -0500
Subject: [PATCH 3/6] docs: router guide review feedback changes
---
aio/content/guide/router.md | 108 +++++++++++++++++++++---------------
1 file changed, 62 insertions(+), 46 deletions(-)
diff --git a/aio/content/guide/router.md b/aio/content/guide/router.md
index 8bd3bedd62d1..f30885e7e0cc 100644
--- a/aio/content/guide/router.md
+++ b/aio/content/guide/router.md
@@ -148,12 +148,9 @@ If you need to see what events are happening during the navigation lifecycle, th
### Router outlet
-Given this configuration, when the browser URL for this application becomes `/heroes`,
-the router matches that URL to the route path `/heroes` and displays the `HeroListComponent`
-_after_ a `RouterOutlet` that you've placed in the host component's template.
-
-The `RouterOutlet` is a directive from the router library that marks
-the spot in the template where the router should display the components for that outlet.
+The `RouterOutlet` is a directive from the router library that is used like a component.
+It acts as a placeholder that marks the spot in the template where the router should
+display the components for that outlet.
@@ -162,6 +159,10 @@ the spot in the template where the router should display the components for that
+Given the configuration above, when the browser URL for this application becomes `/heroes`,
+the router matches that URL to the route path `/heroes` and displays the `HeroListComponent`
+as a sibling element to the `RouterOutlet` that you've placed in the host component's template.
+
{@a basics-router-links}
{@a router-link}
@@ -191,21 +192,18 @@ The router resolves that array into a complete URL.
{@a router-link-active}
-### Active Router Links
+### Active router links
-The **`RouterLinkActive`** directive on each anchor tag helps visually distinguish the anchor for the currently selected "active" route.
+The `RouterLinkActive` directive toggles css classes for active `RouterLink` bindings based on the current `RouterState`.
-On each anchor tag, you also see [property bindings](guide/template-syntax#property-binding) to the `RouterLinkActive` directive that look like `routerLinkActive="..."`.
+On each anchor tag, you see a [property binding](guide/template-syntax#property-binding) to the `RouterLinkActive` directive that look like `routerLinkActive="..."`.
The template expression to the right of the equals (=) contains a space-delimited string of CSS classes
-that the Router will add when this link is active (and remove when the link is inactive).
-You can also set the `RouterLinkActive` directive to a string of classes such as `[routerLinkActive]="'active fluffy'"`
-or bind it to a component property that returns such a string.
+that the Router will add when this link is active (and remove when the link is inactive). You set the `RouterLinkActive`
+directive to a string of classes such as `[routerLinkActive]="'active fluffy'"` or bind it to a component
+property that returns such a string.
-The `RouterLinkActive` directive toggles css classes for active `RouterLink`s based on the current `RouterState`.
-This cascades down through each level of the route tree, so parent and child router links can be active at the same time.
-To override this behavior, you can bind to the `[routerLinkActiveOptions]` input binding with the `{ exact: true }` expression.
-By using `{ exact: true }`, a given `RouterLink` will only be active if its URL is an exact match to the current URL.
+Active route links cascade down through each level of the route tree, so parent and child router links can be active at the same time. To override this behavior, you can bind to the `[routerLinkActiveOptions]` input binding with the `{ exact: true }` expression. By using `{ exact: true }`, a given `RouterLink` will only be active if its URL is an exact match to the current URL.
{@a basics-router-state}
@@ -440,8 +438,18 @@ During each navigation, the `Router` emits navigation events through the `Router
- An [event](api/router/ChildActivationStart) triggered when the Router begins activating a route's children. This
- event receives the Route config for the parent route.
+ An [event](api/router/ChildActivationStart) triggered when the Router begins activating a route's children.
+
+
+
+
+
+
+ ActivationStart
+
+
+
+ An [event](api/router/ActivationStart) triggered when the Router begins activating a route.
@@ -485,8 +493,18 @@ During each navigation, the `Router` emits navigation events through the `Router
- An [event](api/router/ChildActivationEnd) triggered when the Router finishes activating a route's children. This
- event receives the Route config for the parent route.
+ An [event](api/router/ChildActivationEnd) triggered when the Router finishes activating a route's children.
+
+
+
+
+
+
+ ActivationEnd
+
+
+
+ An [event](api/router/ActivationStart) triggered when the Router finishes activating a route.
@@ -537,8 +555,7 @@ During each navigation, the `Router` emits navigation events through the `Router
-These events are logged to the console when the `enableTracing` option is enabled also. Because the events are provided as an `Observable`, you can `filter()` for events of interest and `subscribe()` to them to make decisions based on the sequence of events in the navigation process.
-
+These events are logged to the console when the `enableTracing` option is enabled also. For an example of filtering router navigation events, visit the [router section](guide/observables-in-angular#router) of the [Observables in Angular](guide/observables-in-angular) guide.
{@a basics-summary}
@@ -891,8 +908,13 @@ Replace the contents of each component with the sample HTML below.
### Register Router and Routes
-In order to use the Router, you must first register the `RouterModule` from the `@angular/router` package. Define an array of routes, `appRoutes`, and pass them to the `RouterModule.forRoot` method. It returns a module, containing the configured `Router` service provider, plus other providers that the routing library requires. Once the application is bootstrapped, the `Router` performs the initial navigation based on the current browser URL.
+In order to use the Router, you must first register the `RouterModule` from the `@angular/router` package. Define an array of routes, `appRoutes`, and pass them to the `RouterModule.forRoot()` method. It returns a module, containing the configured `Router` service provider, plus other providers that the routing library requires. Once the application is bootstrapped, the `Router` performs the initial navigation based on the current browser URL.
+
+
+
+ **Note:** The `RouterModule.forRoot` method is a pattern used to register application-wide providers. Read more about application-wide providers in the [Singleton services](guide/singleton-services#forroot) guide.
+
@@ -900,25 +922,25 @@ In order to use the Router, you must first register the `RouterModule` from the
-Adding the configured `RouterModule` to the `AppModule` is sufficient for simple route configurations. As the application grows, you'll want to refactor the routing configuration into a separate file and create a **[Routing Module](#routing-module)**, a special type of `Service Module` dedicated to the purpose of routing in feature modules.
+Adding the configured `RouterModule` to the `AppModule` is sufficient for simple route configurations. As the application grows, you'll want to [refactor the routing configuration](#refactor-the-routing-configuration-into-a-routing-module) into a separate file and create a **[Routing Module](#routing-module)**, a special type of `Service Module` dedicated to the purpose of routing in feature modules.
-Providing the `RouterModule` in the `AppModule` makes the Router available everywhere in the application.
+Registering the `RouterModule.forRoot()` in the `AppModule` imports makes the `Router` service available everywhere in the application.
{@a shell}
### Add the Router Outlet
-The root `AppComponent` is the application shell. It has a title, a navigation bar with two links, and a *router outlet* where the router swaps views on and off the page. Here's what you get:
+The root `AppComponent` is the application shell. It has a title, a navigation bar with two links, and a router outlet where the router swaps components on and off the page. Here's what you get:
-The `router-outlet` serves as a placeholder when the routed components will be rendered below it.
+The router outlet serves as a placeholder when the routed components will be rendered below it.
{@a shell-template}
@@ -3021,7 +3043,7 @@ That's not always the right thing to do.
* You might want to save pending changes before leaving a component.
* You might ask the user if it's OK to discard pending changes rather than save them.
-You can add _guards_ to the route configuration to handle these scenarios.
+You add _guards_ to the route configuration to handle these scenarios.
A guard's return value controls the router's behavior:
@@ -3031,15 +3053,10 @@ A guard's return value controls the router's behavior:
-
-
-The guard can also tell the router to navigate elsewhere, effectively canceling the current navigation.
-
+**Note:** The guard can also tell the router to navigate elsewhere, effectively canceling the current navigation.
-
-
The guard *might* return its boolean answer synchronously.
But in many cases, the guard can't produce an answer synchronously.
The guard could ask the user a question, save changes to the server, or fetch fresh data.
@@ -3048,6 +3065,12 @@ These are all asynchronous operations.
Accordingly, a routing guard can return an `Observable` or a `Promise` and the
router will wait for the observable to resolve to `true` or `false`.
+
+
+**Note:** The observable provided to the Router _must_ also complete. If the observable does not complete, the navigation will not continue.
+
+
+
The router supports multiple guard interfaces:
* [`CanActivate`](api/router/CanActivate) to mediate navigation *to* a route.
@@ -3223,11 +3246,11 @@ feature module, a dashboard route and two unfinished components to manage crises
-
+
-
+
@@ -3251,12 +3274,8 @@ feature module, a dashboard route and two unfinished components to manage crises
-Although the admin dashboard `RouterLink` is an empty path route in the `AdminComponent`, it
-is considered a match to any route within the admin feature area.
-You only want the `Dashboard` link to be active when the user visits that route.
-Adding an additional binding to the `Dashboard` routerLink,
-`[routerLinkActiveOptions]="{ exact: true }"`, marks the `./` link as active when
-the user navigates to the `/admin` URL and not when navigating to any of the child routes.
+Although the admin dashboard `RouterLink` only contains a relative slash without an additional URL segment, it
+is considered a match to any route within the admin feature area. You only want the `Dashboard` link to be active when the user visits that route. Adding an additional binding to the `Dashboard` routerLink,`[routerLinkActiveOptions]="{ exact: true }"`, marks the `./` link as active when the user navigates to the `/admin` URL and not when navigating to any of the child routes.
@@ -3715,7 +3734,7 @@ It will be there when the `CrisisDetailComponent` ask for it.
-**Three critical points**
+**Two critical points**
1. The router's `Resolve` interface is optional.
The `CrisisDetailResolverService` doesn't inherit from a base class.
@@ -3725,9 +3744,6 @@ The router looks for that method and calls it if found.
Don't worry about all the ways that the user could navigate away.
That's the router's job. Write this class and let the router take it from there.
-1. The observable provided to the Router _must_ complete.
-If the observable does not complete, the navigation will not continue.
-
The relevant *Crisis Center* code for this milestone follows.
From bef3c45cc7f6413895fe088dc6e16672461fefcc Mon Sep 17 00:00:00 2001
From: Brandon Roberts
Date: Wed, 12 Sep 2018 10:12:41 -0500
Subject: [PATCH 4/6] docs: clean up providedIn: 'root' syntax for router
examples
---
.../examples/router/src/app/auth/auth.guard.1.ts | 3 +--
.../examples/router/src/app/auth/auth.guard.2.ts | 3 +--
.../examples/router/src/app/auth/auth.guard.3.ts | 2 +-
.../examples/router/src/app/auth/auth.guard.4.ts | 2 +-
.../examples/router/src/app/auth/auth.guard.ts | 2 +-
.../examples/router/src/app/auth/auth.service.ts | 2 +-
.../router/src/app/can-deactivate.guard.1.ts | 2 +-
.../router/src/app/can-deactivate.guard.ts | 2 +-
.../crisis-detail-resolver.service.1.ts | 2 +-
.../crisis-detail-resolver.service.ts | 15 +++++++++------
.../src/app/crisis-center/crisis.service.ts | 2 +-
.../examples/router/src/app/dialog.service.ts | 2 +-
.../app/selective-preloading-strategy.service.ts | 2 +-
13 files changed, 21 insertions(+), 20 deletions(-)
diff --git a/aio/content/examples/router/src/app/auth/auth.guard.1.ts b/aio/content/examples/router/src/app/auth/auth.guard.1.ts
index a55d52100c00..1caa4ef0e78a 100644
--- a/aio/content/examples/router/src/app/auth/auth.guard.1.ts
+++ b/aio/content/examples/router/src/app/auth/auth.guard.1.ts
@@ -1,10 +1,9 @@
// #docregion
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
-import { Observable } from 'rxjs/Observable';
@Injectable({
- providedIn: 'root'
+ providedIn: 'root',
})
export class AuthGuard implements CanActivate {
canActivate(
diff --git a/aio/content/examples/router/src/app/auth/auth.guard.2.ts b/aio/content/examples/router/src/app/auth/auth.guard.2.ts
index 08c289a718c8..2ce03c8c9f83 100644
--- a/aio/content/examples/router/src/app/auth/auth.guard.2.ts
+++ b/aio/content/examples/router/src/app/auth/auth.guard.2.ts
@@ -1,12 +1,11 @@
// #docregion
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
-import { Observable } from 'rxjs/Observable';
import { AuthService } from './auth.service';
@Injectable({
- providedIn: 'root'
+ providedIn: 'root',
})
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}
diff --git a/aio/content/examples/router/src/app/auth/auth.guard.3.ts b/aio/content/examples/router/src/app/auth/auth.guard.3.ts
index 5bb0c47a1533..97dc4546b48c 100644
--- a/aio/content/examples/router/src/app/auth/auth.guard.3.ts
+++ b/aio/content/examples/router/src/app/auth/auth.guard.3.ts
@@ -9,7 +9,7 @@ import {
import { AuthService } from './auth.service';
@Injectable({
- providedIn: 'root'
+ providedIn: 'root',
})
export class AuthGuard implements CanActivate, CanActivateChild {
constructor(private authService: AuthService, private router: Router) {}
diff --git a/aio/content/examples/router/src/app/auth/auth.guard.4.ts b/aio/content/examples/router/src/app/auth/auth.guard.4.ts
index f460682c5f49..feca8d2eb015 100644
--- a/aio/content/examples/router/src/app/auth/auth.guard.4.ts
+++ b/aio/content/examples/router/src/app/auth/auth.guard.4.ts
@@ -11,7 +11,7 @@ import {
import { AuthService } from './auth.service';
@Injectable({
- providedIn: 'root'
+ providedIn: 'root',
})
export class AuthGuard implements CanActivate, CanActivateChild {
constructor(private authService: AuthService, private router: Router) {}
diff --git a/aio/content/examples/router/src/app/auth/auth.guard.ts b/aio/content/examples/router/src/app/auth/auth.guard.ts
index da9f79cb2b05..8f7d863cc1b9 100644
--- a/aio/content/examples/router/src/app/auth/auth.guard.ts
+++ b/aio/content/examples/router/src/app/auth/auth.guard.ts
@@ -11,7 +11,7 @@ import {
import { AuthService } from './auth.service';
@Injectable({
- providedIn: 'root'
+ providedIn: 'root',
})
export class AuthGuard implements CanActivate, CanActivateChild, CanLoad {
constructor(private authService: AuthService, private router: Router) {}
diff --git a/aio/content/examples/router/src/app/auth/auth.service.ts b/aio/content/examples/router/src/app/auth/auth.service.ts
index 6627d28fd61a..280caa14dacc 100644
--- a/aio/content/examples/router/src/app/auth/auth.service.ts
+++ b/aio/content/examples/router/src/app/auth/auth.service.ts
@@ -5,7 +5,7 @@ import { Observable, of } from 'rxjs';
import { tap, delay } from 'rxjs/operators';
@Injectable({
- providedIn: 'root'
+ providedIn: 'root',
})
export class AuthService {
isLoggedIn = false;
diff --git a/aio/content/examples/router/src/app/can-deactivate.guard.1.ts b/aio/content/examples/router/src/app/can-deactivate.guard.1.ts
index 45ccd1726d7e..b490093d4290 100644
--- a/aio/content/examples/router/src/app/can-deactivate.guard.1.ts
+++ b/aio/content/examples/router/src/app/can-deactivate.guard.1.ts
@@ -8,7 +8,7 @@ import { CanDeactivate,
import { CrisisDetailComponent } from './crisis-center/crisis-detail/crisis-detail.component';
@Injectable({
- providedIn: 'root'
+ providedIn: 'root',
})
export class CanDeactivateGuard implements CanDeactivate {
diff --git a/aio/content/examples/router/src/app/can-deactivate.guard.ts b/aio/content/examples/router/src/app/can-deactivate.guard.ts
index 1c947440a34a..41a93a2af433 100644
--- a/aio/content/examples/router/src/app/can-deactivate.guard.ts
+++ b/aio/content/examples/router/src/app/can-deactivate.guard.ts
@@ -8,7 +8,7 @@ export interface CanComponentDeactivate {
}
@Injectable({
- providedIn: 'root'
+ providedIn: 'root',
})
export class CanDeactivateGuard implements CanDeactivate {
canDeactivate(component: CanComponentDeactivate) {
diff --git a/aio/content/examples/router/src/app/crisis-center/crisis-detail-resolver.service.1.ts b/aio/content/examples/router/src/app/crisis-center/crisis-detail-resolver.service.1.ts
index ded64f12cd60..c87b9343fc9a 100644
--- a/aio/content/examples/router/src/app/crisis-center/crisis-detail-resolver.service.1.ts
+++ b/aio/content/examples/router/src/app/crisis-center/crisis-detail-resolver.service.1.ts
@@ -2,7 +2,7 @@
import { Injectable } from '@angular/core';
@Injectable({
- providedIn: 'root'
+ providedIn: 'root',
})
export class CrisisDetailResolverService {
diff --git a/aio/content/examples/router/src/app/crisis-center/crisis-detail-resolver.service.ts b/aio/content/examples/router/src/app/crisis-center/crisis-detail-resolver.service.ts
index b801f9ead912..eed3b2a19072 100644
--- a/aio/content/examples/router/src/app/crisis-center/crisis-detail-resolver.service.ts
+++ b/aio/content/examples/router/src/app/crisis-center/crisis-detail-resolver.service.ts
@@ -1,16 +1,19 @@
// #docregion
import { Injectable } from '@angular/core';
-import { Router, Resolve, RouterStateSnapshot,
- ActivatedRouteSnapshot } from '@angular/router';
-import { Observable, of, EMPTY as empty } from 'rxjs';
-import { mergeMap, take } from 'rxjs/operators';
+import {
+ Router, Resolve,
+ RouterStateSnapshot,
+ ActivatedRouteSnapshot
+} from '@angular/router';
+import { Observable, of, EMPTY } from 'rxjs';
+import { mergeMap, take } from 'rxjs/operators';
import { CrisisService } from './crisis.service';
import { Crisis } from './crisis';
@Injectable({
- providedIn: 'root'
+ providedIn: 'root',
})
export class CrisisDetailResolverService implements Resolve {
constructor(private cs: CrisisService, private router: Router) {}
@@ -25,7 +28,7 @@ export class CrisisDetailResolverService implements Resolve {
return of(crisis);
} else { // id not found
this.router.navigate(['/crisis-center']);
- return empty;
+ return EMPTY;
}
})
);
diff --git a/aio/content/examples/router/src/app/crisis-center/crisis.service.ts b/aio/content/examples/router/src/app/crisis-center/crisis.service.ts
index 417ed49d3467..a1ebc42256c7 100644
--- a/aio/content/examples/router/src/app/crisis-center/crisis.service.ts
+++ b/aio/content/examples/router/src/app/crisis-center/crisis.service.ts
@@ -9,7 +9,7 @@ import { Crisis } from './crisis';
import { CRISES } from './mock-crises';
@Injectable({
- providedIn: 'root'
+ providedIn: 'root',
})
export class CrisisService {
static nextCrisisId = 100;
diff --git a/aio/content/examples/router/src/app/dialog.service.ts b/aio/content/examples/router/src/app/dialog.service.ts
index 218822bc0e07..e0ed6760cb4f 100644
--- a/aio/content/examples/router/src/app/dialog.service.ts
+++ b/aio/content/examples/router/src/app/dialog.service.ts
@@ -8,7 +8,7 @@ import { Observable, of } from 'rxjs';
* TODO: better modal implementation that doesn't use window.confirm
*/
@Injectable({
- providedIn: 'root'
+ providedIn: 'root',
})
export class DialogService {
/**
diff --git a/aio/content/examples/router/src/app/selective-preloading-strategy.service.ts b/aio/content/examples/router/src/app/selective-preloading-strategy.service.ts
index b1065b78af21..a22ac0227e16 100644
--- a/aio/content/examples/router/src/app/selective-preloading-strategy.service.ts
+++ b/aio/content/examples/router/src/app/selective-preloading-strategy.service.ts
@@ -4,7 +4,7 @@ import { PreloadingStrategy, Route } from '@angular/router';
import { Observable, of } from 'rxjs';
@Injectable({
- providedIn: 'root'
+ providedIn: 'root',
})
export class SelectivePreloadingStrategyService implements PreloadingStrategy {
preloadedModules: string[] = [];
From 7074c0fbafd1fc3bb033f74692ca5ad65f785b98 Mon Sep 17 00:00:00 2001
From: Brandon Roberts
Date: Wed, 12 Sep 2018 11:20:07 -0500
Subject: [PATCH 5/6] docs: add section on router integration
---
aio/content/guide/router.md | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)
diff --git a/aio/content/guide/router.md b/aio/content/guide/router.md
index f30885e7e0cc..26f04ac13720 100644
--- a/aio/content/guide/router.md
+++ b/aio/content/guide/router.md
@@ -96,7 +96,7 @@ When the browser's URL changes, that router looks for a corresponding `Route`
from which it can determine the component to display.
A router has no routes until you configure it.
-The following example creates four route definitions, configures the router via the `RouterModule.forRoot` method,
+The following example creates five route definitions, configures the router via the `RouterModule.forRoot` method,
and adds the result to the `AppModule`'s `imports` array.
@@ -1275,6 +1275,22 @@ The **Routing Module** has several characteristics:
* Provides a well-known location for routing service providers including guards and resolvers.
* Does **not** declare components.
+{@a integrate-routing}
+
+### Integrate routing with your app
+
+The sample routing application does not include routing by default.
+When you use the CLI to create a project that will use routing, set the `--routing` option for the project or app, and for each NgModule.
+When you create or initialize a new project (using the CLI new command) or a new app (using the generate app command), specify the `--routing` option to include the `@angular/router` npm package and create a file named `app-routing.module.ts`.
+You can then use routing in any NgModule that you add to the project or app.
+To generate an NgModule that will use routing, specify the `--routing` option in the generate module command:
+
+```sh
+ng generate module my-module --routing
+```
+
+This creates a separate file named `my-module-routing.module.ts` to store the NgModule's routes.
+The file includes an empty `Routes` object that you can fill with routes to different components and NgModules.
{@a routing-refactor}
From 74cbf94225ed7002ca74f6426438d73219fe292a Mon Sep 17 00:00:00 2001
From: Brandon Roberts
Date: Wed, 12 Sep 2018 13:06:02 -0500
Subject: [PATCH 6/6] docs: update routing integration section based on
feedback
---
aio/content/guide/router.md | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/aio/content/guide/router.md b/aio/content/guide/router.md
index 26f04ac13720..ae109eb74c8e 100644
--- a/aio/content/guide/router.md
+++ b/aio/content/guide/router.md
@@ -1281,9 +1281,10 @@ The **Routing Module** has several characteristics:
The sample routing application does not include routing by default.
When you use the CLI to create a project that will use routing, set the `--routing` option for the project or app, and for each NgModule.
-When you create or initialize a new project (using the CLI new command) or a new app (using the generate app command), specify the `--routing` option to include the `@angular/router` npm package and create a file named `app-routing.module.ts`.
+When you create or initialize a new project (using the CLI `new` command) or a new app (using the `generate app` command), specify the `--routing` option. This tells the CLI to include the `@angular/router` npm package and create a file named `app-routing.module.ts`.
You can then use routing in any NgModule that you add to the project or app.
-To generate an NgModule that will use routing, specify the `--routing` option in the generate module command:
+
+For example, the following command generates an NgModule that can use routing.
```sh
ng generate module my-module --routing