New Angular2 router configuration

南笙酒味 提交于 2019-11-27 08:47:46
Günter Zöchbauer

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

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

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 ;-)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!