Angular 4懒加载和路由不起作用

5

我有一个包含应用路由的模块。其中一个路由是懒加载模块。

问题是,这个懒加载模块内部有子组件的路由。但是在我的路由配置中,这些路由不会显示出来......所以当我调用懒加载模块时,屏幕上什么也没有显示。

以下是我的路由配置(主模块):

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"
  }
]

你能帮我吗?


你有没有在控制台看到任何“未找到模块”或类似的错误? - Shivam Mishra
2个回答

9
问题出在我在惰性加载模块上声明路由的方式上:
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]
            }
        ]
    }
]
CalendarComponentpath 必须更改为:
path: 'calendar', // wrong
component: CalendarComponent,
...

以下是相关内容:

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

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

0
在路由的主模块中,您使用了forChild来加载路径,没有根路由来加载子路由。
 @NgModule({
    imports: [
    RouterModule.forChild(MODULE_ROUTES)
  ]})

相比之下,你应该使用

@NgModule({
    imports: [
    RouterModule.forRoot(MODULE_ROUTES)
 ]})

关于延迟加载,还有一件重要的事情需要记住,那就是你应该使用核心模块的 **ModuleWithProviders**。

示例

延迟加载模块路由

import { NgModule, ModuleWithProviders } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [{
    path: '',
    component: ConsumerComponent,
    children: [{
        path: '',
        component: DashboardComponent
}]

export const ConsumerRoutingModule: ModuleWithProviders = RouterModule.forChild(routes);

主模块

@NgModule({
    imports: ['ConsumerRoutingModule']
})

有一篇关于懒加载的有趣博客: 懒加载教程


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接