Unable to redirect to Lazy Loaded module as default route/url on server start up

前端 未结 3 1279
Happy的楠姐
Happy的楠姐 2021-01-25 06:07

Good day folks,

I am having this issue in my Angular 2 application upon server startup (npm start).

I redirect the base route or base path to

相关标签:
3条回答
  • 2021-01-25 06:46

    The () => notation actually worked for me. Below is my code regarding how I fixed it just by editing my routes.ts file

    app/routes.ts

    import { UserModule } from './user/user.module';
    
    export const appRoutes:Routes = [
      { path: 'user', loadChildren: () => UserModule},
      { path: 'login', component: LoginComponent},    
      { path: '', redirectTo: 'user/login', pathMatch: 'full'}
    ]
    
    0 讨论(0)
  • 2021-01-25 06:48

    Try changing your user.module.ts like this :

    @NgModule({
    imports: [
        CommonModule,
        RouterModule.forChild(userRoutes)
    ],
    declarations: [
        LoginComponent
    ],
    exports: [RouterModule], // Add this line
    providers: [UserAuthService]
    })
    
    export class UserModule {}
    

    And routes.ts :

    export const appRoutes:Routes = [
        { path: 'user', loadChildren:  'user/user.module#UserModule'},  
        { path: '', redirectTo: 'user', pathMatch: 'full'}
    ]
    

    Also user.routes.ts :

    export const userRoutes = [
        { path: '', redirectTo: 'login', pathMatch: 'full'},
        { path: 'login', component: LoginComponent }
    ]
    
    0 讨论(0)
  • 2021-01-25 07:10

    Edit: Since Angular 8, the way to lazy load modules is using dynamic imports such as:

     export const appRoutes:Routes = [
        { path: 'user', loadChildren:  () => import('path-to-module').then(m => m.UserModule)},  
        { path: '', redirectTo: 'user', pathMatch: 'full'}
    ]
    

    The accepted answer is not the correct way to implement lazyLoading at all. This should be the routing mechanism:

    app.routes.ts

    export const appRoutes:Routes = [
    { path: 'user', loadChildren:  'user/user.module#UserModule'},  
    { path: '', redirectTo: 'user', pathMatch: 'full'}
    

    ]

    user.routes.ts

    export const userRoutes = [
     {
    path: '',
    children: [
      { path: '', redirectTo: 'login', pathMatch: 'full' },
      {
        path: 'login',
        component: LoginComponent
      }]
    

    } ]

    ibenjelloun's answer is correct. Since you mentioned somehow it doesn't seem to work for you, I have given an alternative way with child routes.

    0 讨论(0)
提交回复
热议问题