Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度路由器-延迟加载模块can';无法将URL与参数匹配_Angular_Angular2 Routing_Angular Routing - Fatal编程技术网

Angular 角度路由器-延迟加载模块can';无法将URL与参数匹配

Angular 角度路由器-延迟加载模块can';无法将URL与参数匹配,angular,angular2-routing,angular-routing,Angular,Angular2 Routing,Angular Routing,我以前有下面的工作代码,当我的整个应用程序被急切地加载时 //以前的代码 常量homeRoutes:路由=[{ 路径:“”, 组件:HomeComponent, canActivate:[AuthGuard], 儿童:[ {path:'profile',component:ProfileComponent}, {path:'user/:userId',component:ProfileComponent,pathMatch:'full'}, //其他未显示 ] }]; @NGD模块({ 导入:[

我以前有下面的工作代码,当我的整个应用程序被急切地加载时

//以前的代码
常量homeRoutes:路由=[{
路径:“”,
组件:HomeComponent,
canActivate:[AuthGuard],
儿童:[
{path:'profile',component:ProfileComponent},
{path:'user/:userId',component:ProfileComponent,pathMatch:'full'},
//其他未显示
]
}];
@NGD模块({
导入:[RouterModule.forChild(homeRoutes)],
导出:[路由模块]
})
导出类HomeRoutingModule{}
/profile
页面是与
/user/{id}
页面相同的组件,但布局略有不同,因此我对它们的处理略有不同

此后,我将Angular 7升级到Angular 8,并实现了延迟加载,这在我的应用程序中除此之外的任何地方都有效。我想这是因为这条路线不像其他路线有一个参数

这是我新的路由代码

const homeRoutes:路由=[{
路径:“”,
组件:HomeComponent,
canActivate:[AuthGuard],
儿童:[
{path:'user/:id',loadChildren:()=>import('../profile/profile.module')。然后(mod=>mod.ProfileModule)},
{path:'profile',loadChildren:()=>import('../profile/profile.module')。然后(mod=>mod.ProfileModule)},
//其他未显示
]
}];
@NGD模块({
导入:[RouterModule.forChild(homeRoutes)],
导出:[路由模块]
})
导出类HomeRoutingModule{}
配置文件模块具有以下代码:

@NgModule({
进口:[
共享模块,
公共模块,
RouterModule.forChild([
{路径:“”,组件:ProfileComponent}
])
],
声明:[
ProfileComponent,//其他未显示
],
entryComponents:[//未显示内容
})
导出类配置文件模块{}
问题是,当我尝试导航到
/user/5cd466b3cdd9b31594bad5d7
时,出现以下错误:

错误错误:未捕获(承诺中):错误:无法匹配任何路由。URL段:“用户/5cd466b3cdd9b31594bad5d7”

这是怎么回事


编辑:我使概要文件组件非延迟加载(并将其从其模块中删除,将其放在前一个位置-主模块)。我还使路由到它的组件之一非惰性加载(并将其从其模块中删除,将其放在前一个位置-主模块)。然后它成功了。当我使链接到概要文件的组件再次延迟加载时,它停止工作。因此,似乎从延迟加载的组件内部调用this.router.navigate()会导致问题。但是,我不知道为什么。

对我来说,问题是您在HomeRoutingModule中延迟加载配置文件模块两次。尝试这样实现:

家庭路由模块:

const homeRoutes:路由=[{
路径:“”,
组件:HomeComponent,
canActivate:[AuthGuard],
儿童:[
{路径:'用户',
loadChildren:()=>import('../profile/profile.module')。然后(mod=>mod.ProfileModule)},
//其他未显示
]
}];
@NGD模块({
导入:[RouterModule.forChild(homeRoutes)],//如果是根模块,则使用forRoot
导出:[路由模块]
})
导出类HomeRoutingModule{}
档案模块:

@NgModule({
进口:[
共享模块,
公共模块,
RouterModule.forChild([
{path:':id',component:ProfileComponent},
{path:'profile',component:ProfileComponent}
])
],
声明:[
ProfileComponent,//其他未显示
],
entryComponents:[//未显示内容
})
导出类配置文件模块{}
您需要将链接从“个人资料”更改为“用户/个人资料”


我不知道这是否解决了您的问题,但即使没有,它也是一个更干净的实现。

对我来说,问题是您在HomeRoutingModule中延迟加载配置文件模块两次。尝试这样实现:

家庭路由模块:

const homeRoutes:路由=[{
路径:“”,
组件:HomeComponent,
canActivate:[AuthGuard],
儿童:[
{路径:'用户',
loadChildren:()=>import('../profile/profile.module')。然后(mod=>mod.ProfileModule)},
//其他未显示
]
}];
@NGD模块({
导入:[RouterModule.forChild(homeRoutes)],//如果是根模块,则使用forRoot
导出:[路由模块]
})
导出类HomeRoutingModule{}
档案模块:

@NgModule({
进口:[
共享模块,
公共模块,
RouterModule.forChild([
{path:':id',component:ProfileComponent},
{path:'profile',component:ProfileComponent}
])
],
声明:[
ProfileComponent,//其他未显示
],
entryComponents:[//未显示内容
})
导出类配置文件模块{}
您需要将链接从“个人资料”更改为“用户/个人资料”


我不知道这是否能解决你的问题,但即使不能,这是一个更干净的实现。

为了能够从延迟加载模块访问带有参数的路由,您必须在父模块和延迟加载模块中定义路由模式

因此,您的代码变成:

const homeRoutes:路由=[{
路径:“”,
组件:HomeComponent,
canActivate:[AuthGuard],
儿童:[
//声明这一点很重要!
{path:'user',loadChildren:()=>import('../user/user.module')。然后(mod=>mod.UserModule)},
{path:'user/:id',loadChildren:()=>import('../user/user.module')。然后(mod=>mod.UserModule)},
{path:'profile',loadChildren:()=>import('../profile/profile.module')。然后(mod=>mod.ProfileModule)},
//其他未显示
]
}];
然后,在用户路由.module.ts中

<