Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit f056a2d

Browse files
brandonrobertswardbell
authored andcommitted
docs(router): Added content updates to developer guide
closes #1905 Added section for RouterLinkActive Added section for global query params and fragments Added section for RouterState Added wildcard route to example configuration Updated code samples Renamed .guard files to .service Renamed interfaces.ts to can-deactivate-guard.service.ts Removed unused files
1 parent 83ba850 commit f056a2d

30 files changed

+330
-210
lines changed

public/docs/_examples/router/ts/app/app.component.1.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import { ROUTER_DIRECTIVES } from '@angular/router';
1212
template: `
1313
<h1>Component Router</h1>
1414
<nav>
15-
<a [routerLink]="['/crisis-center']">Crisis Center</a>
16-
<a [routerLink]="['/heroes']">Heroes</a>
15+
<a routerLink="/crisis-center" routerLinkActive="active">Crisis Center</a>
16+
<a routerLink="/heroes" routerLinkActive="active">Heroes</a>
1717
</nav>
1818
<router-outlet></router-outlet>
1919
`,

public/docs/_examples/router/ts/app/app.component.2.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import { HeroService } from './heroes/hero.service';
2525
template: `
2626
<h1>Component Router</h1>
2727
<nav>
28-
<a [routerLink]="['/crisis-center']">Crisis Center</a>
29-
<a [routerLink]="['/heroes']">Heroes</a>
28+
<a routerLink="/crisis-center" routerLinkActive="active">Crisis Center</a>
29+
<a routerLink="/heroes" routerLinkActive="active">Heroes</a>
3030
</nav>
3131
<router-outlet></router-outlet>
3232
`,

public/docs/_examples/router/ts/app/app.component.3.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,17 @@ import { HeroService } from './heroes/hero.service';
3131
<a [routerLink]="['/crisis-center/1']">Dragon Crisis</a>
3232
// #enddocregion Dragon-anchor
3333
*/
34+
/* Crisis Center link with optional query params
35+
// #docregion cc-query-params
36+
<a [routerLink]="['/crisis-center', { foo: 'foo' }]">Crisis Center</a>
37+
// #enddocregion cc-query-params
38+
*/
3439
// #docregion template
3540
template: `
3641
<h1 class="title">Component Router</h1>
3742
<nav>
3843
<a [routerLink]="['/crisis-center']">Crisis Center</a>
39-
<a [routerLink]="['/crisis-center/1']">Dragon Crisis</a>
44+
<a [routerLink]="['/crisis-center/1', { foo: 'foo' }]">Dragon Crisis</a>
4045
<a [routerLink]="['/crisis-center/2']">Shark Crisis</a>
4146
</nav>
4247
<router-outlet></router-outlet>

public/docs/_examples/router/ts/app/app.component.4.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import { HeroService } from './heroes/hero.service';
1111
template: `
1212
<h1 class="title">Component Router</h1>
1313
<nav>
14-
<a [routerLink]="['/crisis-center']">Crisis Center</a>
15-
<a [routerLink]="['/heroes']">Heroes</a>
16-
<a [routerLink]="['/crisis-center/admin']">Crisis Admin</a>
14+
<a routerLink="/crisis-center" routerLinkActive="active"
15+
[routerLinkActiveOptions]="{ exact: true }">Crisis Center</a>
16+
<a routerLink="/heroes" routerLinkActive="active">Heroes</a>
17+
<a routerLink="/crisis-center/admin" routerLinkActive="active">Crisis Admin</a>
1718
</nav>
1819
<router-outlet></router-outlet>
1920
`,

public/docs/_examples/router/ts/app/app.component.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import { HeroService } from './heroes/hero.service';
1212
template: `
1313
<h1 class="title">Component Router</h1>
1414
<nav>
15-
<a [routerLink]="['/crisis-center']">Crisis Center</a>
16-
<a [routerLink]="['/heroes']">Heroes</a>
17-
<a [routerLink]="['/crisis-center/admin']">Crisis Admin</a>
18-
<a [routerLink]="['/login']">Login</a>
15+
<a routerLink="/crisis-center" routerLinkActive="active"
16+
routerLinkActiveOptions="{ exact: true }">Crisis Center</a>
17+
<a routerLink="/heroes" routerLinkActive="active">Heroes</a>
18+
<a routerLink="/crisis-center/admin" routerLinkActive="active">Crisis Admin</a>
19+
<a routerLink="/login" routerLinkActive="active">Login</a>
1920
</nav>
2021
<router-outlet></router-outlet>
2122
`,

public/docs/_examples/router/ts/app/app.routes.1.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { provideRouter, RouterConfig } from '@angular/router';
1010
import { HeroListComponent } from './hero-list.component';
1111
import { CrisisCenterComponent } from './crisis-center/crisis-center.component';
1212
import { HeroDetailComponent } from './heroes/hero-detail.component';
13+
import { PageNotFoundComponent } from './not-found.component';
1314
// #enddocregion base-routes
1415

1516
// #docregion
@@ -20,8 +21,9 @@ const routes: RouterConfig = [
2021
{ path: 'heroes', component: HeroListComponent },
2122
// #enddocregion route-defs
2223
// #docregion hero-detail-route
23-
{ path: 'hero/:id', component: HeroDetailComponent }
24+
{ path: 'hero/:id', component: HeroDetailComponent },
2425
// #enddocregion hero-detail-route
26+
{ path: '**', component: PageNotFoundComponent }
2527
];
2628

2729
export const appRouterProviders = [

public/docs/_examples/router/ts/app/app.routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { heroesRoutes } from './heroes/heroes.routes';
77
import { loginRoutes,
88
authProviders } from './login.routes';
99

10-
import { CanDeactivateGuard } from './interfaces';
10+
import { CanDeactivateGuard } from './can-deactivate-guard.service';
1111

1212
export const routes: RouterConfig = [
1313
...heroesRoutes,

public/docs/_examples/router/ts/app/auth.guard.1.ts renamed to public/docs/_examples/router/ts/app/auth-guard.service.1.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// #docregion
2+
import { Injectable } from '@angular/core';
23
import { CanActivate } from '@angular/router';
34

5+
@Injectable()
46
export class AuthGuard implements CanActivate {
57
canActivate() {
68
console.log('AuthGuard#canActivate called');
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// #docregion
2+
import { Injectable } from '@angular/core';
3+
import { CanActivate, Router,
4+
ActivatedRouteSnapshot,
5+
RouterStateSnapshot } from '@angular/router';
6+
import { AuthService } from './auth.service';
7+
8+
@Injectable()
9+
export class AuthGuard implements CanActivate {
10+
constructor(private authService: AuthService, private router: Router) {}
11+
12+
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
13+
if (this.authService.isLoggedIn) { return true; }
14+
15+
// Store the attempted URL for redirecting
16+
this.authService.redirectUrl = state.url;
17+
18+
// Navigate to the login page
19+
this.router.navigate(['/login']);
20+
return false;
21+
}
22+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// #docregion
2+
import { Injectable } from '@angular/core';
3+
import { CanActivate, Router,
4+
ActivatedRouteSnapshot,
5+
RouterStateSnapshot } from '@angular/router';
6+
import { AuthService } from './auth.service';
7+
8+
@Injectable()
9+
export class AuthGuard implements CanActivate {
10+
constructor(private authService: AuthService, private router: Router) {}
11+
12+
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
13+
if (this.authService.isLoggedIn) { return true; }
14+
15+
// Store the attempted URL for redirecting
16+
this.authService.redirectUrl = state.url;
17+
18+
// Create a dummy session id
19+
let sessionId = 123456789;
20+
21+
// Set our navigation extras object
22+
// that contains our global query params and fragment
23+
let navigationExtras = {
24+
queryParams: { 'session_id': sessionId },
25+
fragment: 'anchor'
26+
};
27+
28+
// Navigate to the login page with extras
29+
this.router.navigate(['/login'], navigationExtras);
30+
return false;
31+
}
32+
}

0 commit comments

Comments
 (0)