Angular2 router (@angular/router), how to set default route?

后端 未结 10 1992
广开言路
广开言路 2020-12-02 15:07

How can I set a default route in my @Routes route metadata collection? If you use the angular2 router from @angular/router-deprecated you define the routes in @routeConfig

相关标签:
10条回答
  • 2020-12-02 15:50

    You set path of route is ''. Example for DashboardComponent is load first.

    @Routes([
            { path: '', component: DashboardComponent },
            { path: '/ConfigManager', component: ConfigManagerComponent },
            { path: '/Merge', component: MergeComponent },
            { path: '/ApplicationManagement', component: ApplicationMgmtComponent }
    ])
    

    Hope it help you.

    0 讨论(0)
  • 2020-12-02 15:53

    I just faced the same issue, I managed to make it work on my machine, however the change I did is not the same way it is mentioned in the documentation so it could be an issue of angular version routing module, mine is Angular 7

    It only worked when I changed the lazy module main route entry path with same path as configured at the app-routes.ts

    routes = [{path:'', redirectTo: '\home\default', pathMatch: 'full'},
               {path: '', 
               children: [{
                 path:'home',
                 loadChildren :'lazy module path'      
               }]
    
             }];
    
     routes configured at HomeModule
     const routes = [{path: 'home', redirectTo: 'default', pathMatch: 'full'},
                 {path: 'default', component: MyPageComponent},
                ]
    
     instead of 
     const routes = [{path: '', redirectTo: 'default', pathMatch: 'full'},
                       {path: 'default', component: MyPageComponent},
                    ]
    
    0 讨论(0)
  • With the current version of angular 2 you can't use '/' on a path or give a name to your route. What you can do is create a route file like "app.routes.ts" and import your components, make sure of the path when importing.

    import { Component } from '@angular/core';
    import { DashboardComponent } from './dashboard/dashboard.component';
    import { ConfigManagerComponent } from './configManager/configManager.component';
    import { ApplicationMgmtComponent } from './applicationMgmt/applicationMgmt.component';
    import { MergeComponent } from './merge/merge.component';`
    

    Add:

    import {RouterConfig, provideRouter } from '@angular/router';
    

    Then your routes:

    const routes:RouterConfig = [      
        { path: 'Dashboard', component: DashboardComponent },
        { path: 'ConfigManager', component: ConfigManagerComponent },
        { path: 'Merge', component: MergeComponent },
        { path: 'ApplicationManagement', component: ApplicationMgmtComponent }
     ];
    

    Then export:

    export  const APP_ROUTER_PROVIDERS = [
        provideRouter(routes)]
    

    In your main.ts import APP_ROUTER_PROVIDERS and add bootstrap the router providers to the main.ts like this:

    bootstrap(AppComponent,[APP_ROUTER_PROVIDERS]);
    

    Finally, your link will look like this:

    li class="nav hidden-xs"><a [routerLink]="['./Dashboard']" routerLinkActive="active">Dashboard</a>/li>
    
    0 讨论(0)
  • 2020-12-02 15:54

    The path should be left blank to make it default component.

    { path: '', component: DashboardComponent },
    
    0 讨论(0)
提交回复
热议问题