1

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.

1

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

Yes, I thought of that. But is there any other provision in RC5 that even before going to any route when application load first time, its go and get config. I mean resolve that service call ...
Because in this solution, every time I add new route, I have to add canActivate in to it ... its not elegant
I'm not sure what you mean. Is this about first load of the app with deep links?
I didnt get it fully, but I will try what you suggested here and let you know ...
Thanks for your answer. It seems to be working for one you have one application module but it is not working when you have module for each features in your app. So all routes for root of the applications re-route to init component but when user try to route to another area of the application which has its own module it takes user there without routing to init component.
|
1

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'];

2 Comments

I got your point. But even in this scenario, it is tied with route. My requirement is not to navigate to any path until I receive response from service and load application configuration from server. In above example if I route to path hostname/:id then it will call Comp1Resolver but what if user call hostname/about or any other route. So I also have to configure all the routes with same resolver.
Then you can do what Gunter says. Create one route with a resolver and create childRoutes for /about, /:id etc. Then your urls will be hostname/parent/about hostname/parent/id

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.