简体   繁体   中英

Angular5 - routing redirect issue

I want the following routes to work:

  • /settings/general
  • /settings/changePassword
  • /settings/changeEmail

My issue is that settings/ works also. When the user puts /settings into the address bar I want it to redirect to: "/settings/general"

This is my parent route:

    {
      path: 'settings',
      loadChildren: '@application/user#UserModule'
    }

This is my child routes in a child module

RouterModule.forChild([
      {
        path: '',
        component: LayoutComponent,
        children: [
          {
            path: 'changePassword',
            component: PasswordComponent
          },
          {
            path: 'changeEmail',
            component: EmailComponent
          },
          {
            path: 'general',
            component: GeneralComponent
          }
        ]
      }
    ])

Just add a redirect for the '/' route.

RouterModule.forChild([
      {
        path: '',
        component: LayoutComponent,
        children: [
          {
            path: '',
            redirectTo: 'general',
            pathMatch: 'full',
          },
          {
            path: 'changePassword',
            component: PasswordComponent
          },
          {
            path: 'changeEmail',
            component: EmailComponent
          },
          {
            path: 'general',
            component: GeneralComponent
          }
        ]
      }
    ])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM