Angular 2 Allow Service Before Download

http://plnkr.co/edit/yhQQZxTxB8ZVFysqO6lP?p=preview

How can I guarantee that my AuthService will be allowed before loading the AppComponent?

//main entry point import {bootstrap} from '@angular/platform-browser-dynamic'; import {Http,HTTP_PROVIDERS} from '@angular/http'; import {App} from './app'; import {AuthService} from './auth.service'; import {provide, enableProdMode, APP_INITIALIZER} from '@angular/core'; bootstrap(App, [HTTP_PROVIDERS, provide("isLoggedIn", { useFactory: (auth:AuthService) => () => auth.login(), deps:[HTTP_PROVIDERS], multi: true }), ]) .catch(err => console.error(err)); 
+5
source share
1 answer

You can use the APP_INITIALIZER token to provide your authFactory:

 //main entry point import {bootstrap} from '@angular/platform-browser-dynamic'; import {Http,HTTP_PROVIDERS} from '@angular/http'; import {App} from './app'; import {AuthService} from './auth.service'; import {provide, enableProdMode, APP_INITIALIZER} from '@angular/core'; export function authFactory(auth: AuthService){ return auth.login() } bootstrap(App, [ HTTP_PROVIDERS, { provide: APP_INITIALIZER, useFactory: (auth), deps: [AuthService], multi: true } ]).catch(err => console.error(err)); 
0
source

All Articles