I'm trying to load some data before my application starts. The reason why I need to do it, is because some of the menu has restricted access based on some user condition, for example, based on the user location.
So if the user doesn't have a location yet or it's location isn't enabled to access certain view, I need to block it.
I tried to use Angular Resolve method, without success, this is what I did:
Resolve guard
// The apiService is my own service to make http calls to the server.
@Injectable()
export class AppResolveGuard implements Resolve<any> {
constructor(
private _api: ApiService,
private _store: Store<IStoreInterface>,
) { }
resolve(): any {
return this._api.apiGet('loadData').subscribe(response => {
this._store.dispatch({
type: USER_FETCH_DATA,
payload: response.usuario
});
return response;
});
}
}
Routing
export const routing = [
{
path: '', component: AppComponent, canActivateChild: [AuthGuard], resolve: {app: AppResolveGuard},
children: [
{ path: 'home', component: HomeComponent },
{ path: 'company', canActivate: [LocationGuard], component: CompanyComponent },
]
}
];
The AuthGuard is just going to check for a valid cookie session.
As you can see, the routing I'm trying to block access is the company
routing.
I'm able to prevent access when I first load another page, for example, if the first access is to the page home
and then I navigate to the page company
the validation works fine, because the user data and location is already avaliable.
However, if the first access is to the page company
, it will block all users to navigate there, because at the time the LocationGuard
try to validate the user location, the data isn't avaliable yet.
I also tried to replace Resolve
with CanActivate
, but it never enables the view after the data is avaliable, it stays on a blank page.
Once you setup is complete and run the app and we can see settings object from server appear before bootstrapping process. APP_INITIALIZER is a great way to fetch any data or settings from the server before initializing or bootstrapping angular app. Thank you for reading and If you find this useful, Please give it a clap and help others to find it.
We simply read in all of configuration settings available in config files and start the application. The good news is that similar to the Startup class in .net core, we have APP_INITIALIZER in Angular. Simply put, the APP_INITIALIZER is function that will be executed when an application is initialized.
Simply put, the APP_INITIALIZER is function that will be executed when an application is initialized. So let's get into the example of how we would go about loading an external JSON configuration file into our Angular application when it starts. First, lets define our json configuration file.
App module (as any other Angular module) contains Declarations, Imports, Providers, but also entry component, which is a default AppComponent. The component is then loaded and rendered on the page.
A few things ...
Pre-fetching data before a page loads is a great use of a resolver.
Preventing access to a route is a great use of CanActivate.
Note however that the resolver only works AFTER all guards are checked. So your code will attempt to run the CanActivate before running the resolver.
I think you can use APP_INITIALIZER
to load data before application starts.
Check this article:
https://devblog.dymel.pl/2017/10/17/angular-preload/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With