Angular 4 延迟加载和路由不起作用

标签 angular lazy-loading angular2-routing angular-module

我有一个包含我的应用程序路由的模块。其中一条路线是延迟加载模块。

问题是这个延迟加载模块在内部有一个子组件的路由。但是在我的路由器配置中,这条路由没有出现...所以当我调用惰性模块时,不要在我的屏幕上显示任何内容。

这是我的路由器配置(主模块):

export const MODULE_ROUTES: Route[] =[
    { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
    { path: 'dashboard', component: HomeComponent, canActivate: [AuthGuard] },
    { path: 'calendar', loadChildren: 'app/dashboard/calendar/calendar-module.module#CalendarModuleModule',canActivate: [AuthGuard]}, 
    { path: '**', component: NoPageFoundComponent, pathMatch: 'full' }
]
.
.
.


@NgModule({
    imports: [
        RouterModule.forChild(MODULE_ROUTES)
.
.
.

在我的惰性模块上:

export const MODULE_CALENDAR_ROUTES: Route[] = [
    {
        path: 'calendar', component: CalendarComponent, canActivateChild: [AuthGuard, CalendarGuard],
        children: [
            {
                path: '', component: MainCalendarComponent, canActivateChild: [AuthGuard, CalendarGuard]
            },
            {

                path: 'user', component: EditEventComponent, canActivateChild: [AuthGuard, CalendarGuard]
            }
        ]
    }
]

.
.
.

@NgModule({
    imports: [
        SharedModule,
.
.
.
        RouterModule.forChild(MODULE_CALENDAR_ROUTES)

如果我打印我的路由器配置,这个在我的惰性模块上声明的路由不会显示:

Routes:  [
  {
    "path": "dashboard",
    "canActivate": [
      null
    ]
  },
  {
    "path": "calendar",
    "loadChildren": "app/dashboard/calendar/calendar-module.module#CalendarModuleModule",
    "canActivate": [
      null
    ]
  },
  {
    "path": "**",
    "pathMatch": "full"
  },
  {
    "path": "dashboard"
  }
]

你能帮帮我吗?

最佳答案

问题在于我在惰性模块上声明路由的方式:

export const MODULE_CALENDAR_ROUTES: Route[] = [
    {
        path: 'calendar',
        component: CalendarComponent,
        canActivateChild: [AuthGuard, CalendarGuard],
        children: [
            {
                path: '',
                component: MainCalendarComponent,
                canActivateChild: [AuthGuard, CalendarGuard]
            },
            {

                path: 'user',
                component: EditEventComponent,
                canActivateChild: [AuthGuard, CalendarGuard]
            }
        ]
    }
]

CalendarComponent路径 必须更改为:

path: 'calendar', // wrong
component: CalendarComponent,
...

到下面:

path: '', // right
component: CalendarComponent,
...

感谢 gitter 上的@jotatoledo 帮助我解决了这个问题。

关于Angular 4 延迟加载和路由不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44356025/

相关文章:

typescript - 没有字符串的提供者

javascript - Angular - 卡住第一个选项卡,直到另一个选项卡完全加载

javascript - 延迟加载图像 - noscript 后备代码

javascript - 如何在 Angular 2 中延迟加载模板的一部分?

angular - ElementRef 和@ViewChild,不推荐?

angular - ngOnDestroy 在退出前警告用户

jdbc - 如何在clojure中按部分生成惰性序列?

angular - 在路由 Angular 4 中传递多个参数

angular2 guard 不处理页面刷新

javascript - 如何从 Angular 2 的内部组件中路由?