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

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

提交回复
热议问题