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

后端 未结 10 1989
广开言路
广开言路 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:34

    according to documentation you should just

    { path: '**', component: DefaultLayoutComponent }
    

    on your app-routing.module.ts source: https://angular.io/guide/router

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

    I faced same issue apply all possible solution but finally this solve my problem

    export class AppRoutingModule {
    constructor(private router: Router) {
        this.router.errorHandler = (error: any) => {
            this.router.navigate(['404']); // or redirect to default route
        }
      }
    }
    

    Hope this will help you.

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

    Suppose you want to load RegistrationComponent on load and then ConfirmationComponent on some event click on RegistrationComponent.

    So in appModule.ts, you can write like this.

    RouterModule.forRoot([
          { 
            path: '', 
            redirectTo: 'registration', 
            pathMatch: 'full'
           },
           { 
            path: 'registration', 
            component: RegistrationComponent
           },
          {
            path : 'confirmation',
            component: ConfirmationComponent
          }
        ]) 
    

    OR

    RouterModule.forRoot([
           { 
            path: '', 
            component: RegistrationComponent
           },
          {
            path : 'confirmation',
            component: ConfirmationComponent
          }
        ]) 
    

    is also fine. Choose whatever you like.

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

    Only you need to add other parameter in your route, the parameter is useAsDefault:true. For example, if you want the DashboardComponent as default you need to do this:

    @RouteConfig([
        { path: '/Dashboard', component: DashboardComponent , useAsDefault:true},
        .
        .
        .
        ])
    

    I recomend you to add names to your routes.

    { path: '/Dashboard',name:'Dashboard', component: DashboardComponent , useAsDefault:true}
    
    0 讨论(0)
  • 2020-12-02 15:43

    V2.0.0 and later

    See also see https://angular.io/guide/router#the-default-route-to-heroes

    RouterConfig = [
      { path: '', redirectTo: '/heroes', pathMatch: 'full' },
      { path: 'heroes', component: HeroComponent,
        children: [
          { path: '', redirectTo: '/detail', pathMatch: 'full' },
          { path: 'detail', component: HeroDetailComponent }
        ] 
      }
    ];
    

    There is also the catch-all route

    { path: '**', redirectTo: '/heroes', pathMatch: 'full' },
    

    which redirects "invalid" urls.

    V3-alpha (vladivostok)

    Use path / and redirectTo

    RouterConfig = [
      { path: '/', redirectTo: 'heroes', terminal: true },
      { path: 'heroes', component: HeroComponent,
        children: [
          { path: '/', redirectTo: 'detail', terminal: true },
          { path: 'detail', component: HeroDetailComponent }
        ] 
      }
    ];
    

    RC.1 @angular/router

    The RC router doesn't yet support useAsDefault. As a workaround you can navigate explicitely.

    In the root component

    export class AppComponent {
      constructor(router:Router) {
        router.navigate(['/Merge']);
      }
    }
    

    for other components

    export class OtherComponent {
      constructor(private router:Router) {}
    
      routerOnActivate(curr: RouteSegment, prev?: RouteSegment, currTree?: RouteTree, prevTree?: RouteTree) : void {
        this.router.navigate(['SomeRoute'], curr);
      }
    }
    
    0 讨论(0)
  • 2020-12-02 15:48

    In Angular 2+, you can set route to default page by adding this route to your route module. In this case login is my target route for the default page.

    {path:'',redirectTo:'login', pathMatch: 'full' },
    
    0 讨论(0)
提交回复
热议问题