angular 嵌套惰性路由问题
Issue on angular nested lazy routing
我正在尝试实现嵌套子延迟加载路由,但我遇到了一些关于路由的问题。
这是我的路由代码:-
app.modules.ts 看起来像
RouterModule.forRoot([
{path:'',redirectTo:'home',pathMatch:'full'},
{path:'home',component:HomeComponent},
{path:'pages',loadChildren:'./pages/pages.module#PagesModule' },
{path:'**',component: NotFoundComponent}
],{enableTracing:true})
pages.module.ts 看起来像:-
RouterModule.forChild([
{ path:'', component:PagesComponent,
children:[
{path:'',redirectTo:'user',pathMatch:'full'},
{path:'user',component:UserComponent },
{path:'forms',loadChildren:'./forms/forms.module#CustomFormsModule'}]
}
])
forms.modules.ts:-
RouterModule.forChild([
{path:'',component:FormsComponent,
children:[
{path:'',redirectTo:'general',pathMatch:'full'},
{path:'general',component:GeneralComponent},
{path:'advance',component:AdvanceComponent}
] }
])
当我输入路径 http://localhost:4200/pages - 它会将我重定向到 /pages/general
即使我输入 pages/general 它也会给我通用组件,因为我的路由器模块中没有定义确定的路由。
问题是您在代码中导入了延迟加载的模块
//pages.module.ts
import { CustomFormsModule } from './forms/forms.module';
//...
imports: [
CommonModule,
CustomFormsModule, //<--Don't do that
使用延迟加载模块时,不要显式导入它们,否则路由器会混淆
这是编辑过的 stackblitz
https://stackblitz.com/edit/angular-nested-lazy-route-kahove
我正在尝试实现嵌套子延迟加载路由,但我遇到了一些关于路由的问题。
这是我的路由代码:- app.modules.ts 看起来像
RouterModule.forRoot([
{path:'',redirectTo:'home',pathMatch:'full'},
{path:'home',component:HomeComponent},
{path:'pages',loadChildren:'./pages/pages.module#PagesModule' },
{path:'**',component: NotFoundComponent}
],{enableTracing:true})
pages.module.ts 看起来像:-
RouterModule.forChild([
{ path:'', component:PagesComponent,
children:[
{path:'',redirectTo:'user',pathMatch:'full'},
{path:'user',component:UserComponent },
{path:'forms',loadChildren:'./forms/forms.module#CustomFormsModule'}]
}
])
forms.modules.ts:-
RouterModule.forChild([
{path:'',component:FormsComponent,
children:[
{path:'',redirectTo:'general',pathMatch:'full'},
{path:'general',component:GeneralComponent},
{path:'advance',component:AdvanceComponent}
] }
])
当我输入路径 http://localhost:4200/pages - 它会将我重定向到 /pages/general 即使我输入 pages/general 它也会给我通用组件,因为我的路由器模块中没有定义确定的路由。
问题是您在代码中导入了延迟加载的模块
//pages.module.ts
import { CustomFormsModule } from './forms/forms.module';
//...
imports: [
CommonModule,
CustomFormsModule, //<--Don't do that
使用延迟加载模块时,不要显式导入它们,否则路由器会混淆
这是编辑过的 stackblitz https://stackblitz.com/edit/angular-nested-lazy-route-kahove