Angular
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
export class AuthGuard implements CanActivate {<br />
constructor(auth: AuthService) {<br />
this.auth = auth;<br />
}<br />
canActivate(route, state) {<br />
return Observable<br />
.merge(this.auth.fetchCurrentUser(), Observable.of(true))<br />
.filter(x => x == true);<br />
}<br />
}<br />
Here is our CurrentUserResolver:<br />
import { Injectable } from '@angular/core';<br />
import { Resolve } from "@angular/router";<br />
import { Observable } from 'rxjs/Rx';<br />
import { AuthService } from '../services/AuthService';<br />
@Injectable()<br />
export class CurrentUserResolver implements Resolve {<br />
constructor(auth: AuthService) {<br />
this.auth = auth;<br />
}<br />
resolve(route, state) {<br />
return this.auth.currentUser;<br />
}<br />
}<br />
Section 17.4: Use Guard in app bootstrap<br />
File main.ts (or boot.ts)<br />
Consider the examples above:<br />
1.<br />
2.<br />
Create the guard (where the Guard is created) and<br />
Add guard to route configuration, (where the Guard is configured for route, then<br />
APP_ROUTER_PROVIDERS is exported),<br />
we can couple the bootstrap to Guard as follows<br />
import { bootstrap } from '@angular/platform-browser-dynamic';<br />
import { provide } from '@angular/core';<br />
import { APP_ROUTER_PROVIDERS } from './app.routes';<br />
import { AppComponent } from './app.component';<br />
bootstrap(AppComponent, [<br />
APP_ROUTER_PROVIDERS<br />
])<br />
.then(success => console.log(`Bootstrap success`))<br />
.catch(error => console.log(error));<br />
Section 17.5: Bootstrapping<br />
Now that the routes are defined, we need to let our application know about the routes. To do this, bootstrap the<br />
provider we exported in the previous example.<br />
Find your bootstrap configuration (should be in main.ts, but your mileage may vary).<br />
GoalKicker.com – <strong>Angular</strong> 2 Notes for Professionals 83