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

Commit 0522296

Browse files
docs(router): Added content updates to developer guide
Added section for RouterLinkActive Added section for global query params and fragments Added section for RouterState Updated code samples Renamed .guard files to .service Renamed interfaces.ts to can-deactivate-guard.service.ts Removed unused files
1 parent 08d051d commit 0522296

28 files changed

+295
-191
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ import { HeroService } from './heroes/hero.service';
2828
*/
2929
/* Crisis Center Detail link
3030
// #docregion Dragon-anchor
31-
<a [routerLink]="['/crisis-center/1']">Dragon Crisis</a>
31+
<a [routerLink]="['/crisis-center', 1]">Dragon Crisis</a>
3232
// #enddocregion Dragon-anchor
3333
*/
3434
// #docregion template
3535
template: `
3636
<h1 class="title">Component Router</h1>
3737
<nav>
3838
<a [routerLink]="['/crisis-center']">Crisis Center</a>
39-
<a [routerLink]="['/crisis-center/1']">Dragon Crisis</a>
40-
<a [routerLink]="['/crisis-center/2']">Shark Crisis</a>
39+
<a [routerLink]="['/crisis-center', 1, { foo: 'foo' }]">Dragon Crisis</a>
40+
<a [routerLink]="['/crisis-center', 2]">Shark Crisis</a>
4141
</nav>
4242
<router-outlet></router-outlet>
4343
`,

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.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.setRedirect(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.setRedirect(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+
}

public/docs/_examples/router/ts/app/auth.guard.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)