Angular 2 loadChildren 不与 canActivateChild 一起工作

标签 angular lazy-loading angular-routing

我正在尝试使用 Angular2 来延迟加载一些模块。我可以让它工作,但是当我将它与 canActivateChild 一起使用时,它会阻止导航但加载模块,我不知道我是否需要做任何其他事情,但我只想加载模块,如果我是启用访问路由。例如,这是一个管理仪表板,我只想在用户登录时加载 AdminModule。

这是我的主要模块路由:

export const routing = [
    { path: '', redirectTo: 'home', pathMatch: 'full' },
    { path: 'login', component: LoginComponent },
    {
        path: '',
        canActivateChild: [AuthGuard],
        children: [
            { path: 'home', component: HomeComponent },
            { path: 'adm', loadChildren: './admin/admin.module#AdminModule' },
        ]
    },
    { path: '**', component: NotFoundComponent },
];

这是我的管理路由:

export const adminRouting = [
    { path: '',
        children: [
            { path: 'client', component: ClientComponent },
            { path: 'support', component: SupportComponent },
            // more routes
        ]
    },
];

正如我所说,导航中的所有内容都按预期工作,但当我未登录时,我想阻止加载 lazyLoad 模块。

最佳答案

根据 the routing guide在 Angular 上我相信你必须使用 canLoad 而不是 canActivate

关于Angular 2 loadChildren 不与 canActivateChild 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44590176/

相关文章:

Angular4 Material 输入显示不正确

typescript - 如何为 plunker 创建 Angular2 单个文件?

hibernate - 如何在使用 Hibernate 映射的类中实现 toString()?

asp.net - ASP.NET 中的延迟加载图像

javascript - Lazyload polyfill/Magneto 2 js 错误 "loading-attribute-polyfill.js Uncaught ReferenceError: module is not defined"

Angular 6 build,刷新页面时未找到错误 404

angular - Angular 7 中的错误 EmptyErrorImpl {消息 : "no elements in sequence", 名称: "EmptyError"}

javascript - ng-view 导致页面崩溃

angular - 预加载由 Angular CLI 插入的动态生成的字体文件

angular - 如何在 angular2 中访问父组件中的子组件 HTML 元素值?