My app has api_token
in localstorage, and I want to determine a user using api_token
from my REST before app starts. How can I do that? I've used routeResolvers, but I want to determine a user before app starts instead determine a user when route starts.
Asked
Active
Viewed 2,986 times
1

Paul Androschuk
- 796
- 7
- 9
1 Answers
3
If you just need to run some code before bootstrapping Angular, then you can just... run some code outside of Angular and have that code bootstrap the app.
For example:
// main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
// Here, some code retrieving api_token from localStorage.
// Only call the line below once your code is done.
// You might have to place that line in a callback of some sort.
platformBrowserDynamic().bootstrapModule(AppModule);
But I suspect you want to run code AND pass the retrieved value to Angular. In that case, take a look at APP_INITIALIZER
. See explanation + code sample.

AngularChef
- 13,797
- 8
- 53
- 69
-
Thanks for the post. An alternative to APP_INITIALIZER would be to use localStorage for passing data into Angular. – John Henckel Dec 11 '17 at 19:16