Reputation: 2060
I am writing Angular2 App using RC5.
When AppComponent load, I want application to request config file using http and and once its loaded I want app to navigate to the route specified?
How can I achieve that ?
There is 'canActivate' property on each route, but I would see that it has more to do with if I want to do something every time user navigate to new route. Mostly used for authentication purpose, where you want only few routes to be protected with Authentication but in my case I would like to load configuration when AppComponent load. Not on every route activation or navigation.
Hope I made my self clear what I am trying to achieve.
Upvotes: 1
Views: 522
Reputation: 1488
You should create a resolver that does the request, add the resolver to the route and get the resolved config data in the onInit of your component.
Component.Route:
export const routes: Route[] = [
{ path: ':id', component: Component1, resolve: { data: Comp1Resolver }}
];
Comp1Resolver:
@Injectable()
export class Comp1Resolverimplements Resolve<Config> {
constructor(private configService: ConfigService) { }
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Config> {
return this.configService.get(route.params['id']);
}
}
Component1:
ngOnInit() {
let that = this;
this.route.data.subscribe(data => {
that.config= data['data'];
Upvotes: 1
Reputation: 657721
You can store in the canActivate
guard that the initial re-navigation had been done and for subsequent calls only return true
.
You could configure the router with a single route
{ path: '**', InitComponent }
and in InitComponent
load your configuration, reconfigure the router (pass all routes) and then forward to the path initially requested.
See also Angular 2 Dynamic Route Building & Component Level Permissions
Upvotes: 1