4

Back when using the deprecated router I was able to do a router.config and pass in an object. Thing is, the router itself got configured a bit after the app was started, the object had the same "template" as if I'd used the @RouterConfig. What I'm looking for is if there is a way to config the new router like this. Been looking through the documentation but I'm a bit at a loss since it isn't documented yet.

Edit due to answer

No, I can't use @Routes. Thing is I'm loading the configuration after the construction of the router. Here is a snipped of how I did it with the old router:

       myLoader.loadComponentConfig(configPath)
      .then(components => { self.Components = components;
         components.map(comp => {
             self.RouterComponents.push(
                 {
                     path: '/' + comp.name,
                     component: comp,
                     as: comp.name
                 }
             )});
             router.config(self.RouterComponents);
        });

As you can see, I'm building myself a json object ( RouterComponents ) and then sending it to the router. I'm looking for a way to do the same with the new router, or something alike.

taigi100
  • 2,609
  • 4
  • 23
  • 42
  • I am also facing the similar roadblock. We have created a service which returns the route configurations. I am able to write routeLink using ngFor over data received from service. But we cannot use service inside @Routes decorator. Going through documentation we didn't find any configuration method in router. – Siraj May 17 '16 at 21:35
  • @Siraj You can try ngrx router or use the deprecated one. – taigi100 May 17 '16 at 22:06
  • Maybe wait for this https://github.com/angular/angular/issues/8590 – Siraj May 17 '16 at 22:46

3 Answers3

3

In the new router (>= RC.3) https://angular.io/docs/ts/latest/api/router/index/Router-class.html resetConfig can be used

router.resetConfig([
 { path: 'team/:id', component: TeamCmp, children: [
   { path: 'simple', component: SimpleCmp },
   { path: 'user/:name', component: UserCmp }
 ] }
]);

You might need to provide some dummy router configuration to not get errors on application startup.

https://github.com/angular/angular/issues/11437#issuecomment-245995186 provides an RC.6 Plunker

unitario
  • 6,295
  • 4
  • 30
  • 43
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
0

I think It's better like that :

In the main.ts :

import { ROUTER_PROVIDERS } from 'angular2/router';

bootstrap(AppComponent, [
    ROUTER_PROVIDERS,
    provide(LocationStrategy, { useClass: HashLocationStrategy })
]);

In your component.ts :

import { Router, RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from 'angular2/router';
import { APP_ROUTES } from './route.config';

@RouteConfig(APP_ROUTES)

And creat a file route.config.ts :

import { Route } from 'angular2/router';

import { HomeComponent } from './home/home.component';
import { TableComponent } from './table/table.component';

export var Routes = {
    home: new Route({
        path: '/home',
        component: HomeComponent,
        name: 'Home',
        useAsDefault: true,
        data: {
            title: 'Home'
        }
    }),
    table: new Route({
        path: '/table/:table',
        component: TableComponent,
        name: 'Table'
    })
};

export const APP_ROUTES = Object.keys(Routes).map(r => Routes[r]);
Aastal
  • 350
  • 2
  • 8
  • 1. That's the deprecated router. 2. I have a jsom file with the routes that gets updated from time to time so I need to be able to reconfigure it. – taigi100 May 17 '16 at 12:51
-1

In fact, in short you need to use @Routes instead of @RouterConfig. Here is a sample:

@Routes([
  {path: '/crisis-center', component: CrisisListComponent},
  {path: '/heroes',        component: HeroListComponent},
  {path: '*',        component: CrisisListComponent}
])
export class AppComponent { }

See this doc on angular.io for more details:

You can notice that it remains some typos in this doc since this chapter is a work in progress ;-)

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360