Angular 4 延迟加载和路由不起作用
Angular 4 Lazy Loading and Routes not working
我有一个包含我的应用程序路由的模块。其中一条路线是延迟加载模块。
问题是这个延迟加载模块在内部有一个子组件的路由。但是在我的路由器配置中,这条路由没有出现...所以当我调用惰性模块时,屏幕上不会显示任何内容。
这是我的路由器配置(主模块):
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
必须更改为:
path: 'calendar', // wrong
component: CalendarComponent,
...
到下面:
path: '', // right
component: CalendarComponent,
...
感谢 gitter 上的@jotatoledo 帮助我解决了这个问题。
在路由的主要模块中,您使用了 forChild 来加载路径,没有任何根路由器来加载子路径。
@NgModule({
imports: [
RouterModule.forChild(MODULE_ROUTES)
]})
而不是你应该使用
@NgModule({
imports: [
RouterModule.forRoot(MODULE_ROUTES)
]})
还有一件重要的事情应该记住,你应该使用核心模块的**ModuleWithProviders**延迟加载
EX
惰性模块路由
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);
main_module
@NgModule({
imports: ['ConsumerRoutingModule']
})
有一篇关于延迟加载的有趣博客:
lazyload tutorial
我有一个包含我的应用程序路由的模块。其中一条路线是延迟加载模块。
问题是这个延迟加载模块在内部有一个子组件的路由。但是在我的路由器配置中,这条路由没有出现...所以当我调用惰性模块时,屏幕上不会显示任何内容。
这是我的路由器配置(主模块):
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
必须更改为:
path: 'calendar', // wrong
component: CalendarComponent,
...
到下面:
path: '', // right
component: CalendarComponent,
...
感谢 gitter 上的@jotatoledo 帮助我解决了这个问题。
在路由的主要模块中,您使用了 forChild 来加载路径,没有任何根路由器来加载子路径。
@NgModule({
imports: [
RouterModule.forChild(MODULE_ROUTES)
]})
而不是你应该使用
@NgModule({
imports: [
RouterModule.forRoot(MODULE_ROUTES)
]})
还有一件重要的事情应该记住,你应该使用核心模块的**ModuleWithProviders**延迟加载
EX
惰性模块路由
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);
main_module
@NgModule({
imports: ['ConsumerRoutingModule']
})
有一篇关于延迟加载的有趣博客: lazyload tutorial