New Angular2 router configuration

后端 未结 3 985
迷失自我
迷失自我 2020-12-01 15:21

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

3条回答
  •  广开言路
    2020-12-01 15:53

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

提交回复
热议问题