延迟加载模块之间的路由 ion angular 9

Route between lazy loaded modules ion angular 9

我的 AppModule 中声明了 2 个延迟加载模块:

 {
    path: 'dashboard',
    loadChildren: () => import('./modules/dashboards/dashboard.module').then(m => m.DashboardModule),
    canActivate: [ AuthGuard ]
},
{
    path: 'auth',
    loadChildren: () => import('./modules/auth/auth.module').then(m => m.AuthModule)
},
{path: '', redirectTo: 'auth', pathMatch: 'full'},
{path: '**', component: PageNotFoundComponent},

auth 模块包含登录页面。成功登录后,它应该重定向到仪表板。遗憾的是,我无法设法从 auth 模块导航到仪表板。我试过了

 this.router.navigate([ 'dashboard' ]);
 this.router.navigate([ './dashboard' ]);
 this.router.navigate([ '../dashboard' ]);

授权模块路由:

  {
    path: '',
    component: AuthContainerComponent,
    children: [
        {
            path: '',
            redirectTo: 'login',
            pathMatch: 'full',
        },
        {
            path: 'login',
            component: LoginComponent
        }, ...]}

仪表板模块路线:

  {
    path: 'customer',
    loadChildren: () => import('./customer/customer.module').then(m => m.CustomerModule),
    canActivate: [ AuthGuard ],
    data: {
        roles: [ Roles.Customer ]
    },
},
{
    path: 'staff',
    loadChildren: () => import('./staff/staff.module').then(m => m.StaffModule),
},
{path: '', redirectTo: 'customer', pathMatch: 'full'},

知道为什么它不起作用吗?是因为模块是延迟加载的吗?感谢您的帮助!

好吧,完全是我的错。 Auth Guard 是问题所在。

你能试试这个吗?

 {
    path: '',
    loadChildren: () => import('./modules/dashboards/dashboard.module').then(m => m.DashboardModule),
    canActivate: [ AuthGuard ]
},

请删除它,直到问题得到解决:

{path: '', redirectTo: 'auth', pathMatch: 'full'},

应该可以导航到

this.router.navigate([ 'dashboard/customer' ]);
this.router.navigate([ 'dashboard/staff' ]);

但前提是这些模块(员工和客户)在其映射配置中有一个路径:''。

能不能把CustomerModule和StaffModule的代码公开一下? stackblitz 将是完美的 ;-)

您还可以通过以下方式在您的应用程序路由器中激活路由日志记录:

RouterModule.forRoot(routes, {enableTracing: true}),