gpt4 book ai didi

Angular 懒加载路由总是去通配符模块

转载 作者:太空狗 更新时间:2023-10-29 19:36:04 26 4
gpt4 key购买 nike

我正在开发一个具有多个模块的新 Angular 应用程序。我仍在努力让我的路由正确。在下面的(简化的)示例中,我想延迟加载 StoreModule。如果没有给出 url,我希望我的应用程序重定向到 /store。如果提供的 URL 无效,我希望显示我的 NotFoundComponent。然而,在我当前的配置中,始终显示 NotFoundComponent,无论 URL 是什么。你们看到我做错了什么了吗?

这是我的 app.module.ts 文件,如果没有 URL 匹配,我希望它只使用 NotFoundModule 中提供的 RouterModule可以制作。

app.module.ts

@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AuthModule,
RouterModule.forRoot([
{ path: '', redirectTo: 'store', pathMatch: 'full'},
{ path: 'store', loadChildren: 'app/store/store.module#StoreModule'},
{ path: 'login', component: LoginComponent },
]),
LoginModule,
NotfoundModule,
],
bootstrap: [AppComponent],
exports: [RouterModule]
})
export class AppModule { }

这是我的 StoreModule。如果我在我的 app.module.ts 模块中注释掉 NotFoundModule,这一切都会按预期工作。

store.module.ts

@NgModule({
imports: [
AuthModule,
CommonModule,
SharedModule,
RouterModule.forChild([
{ path: '', pathMatch: 'full', redirectTo: 'dashboard' },
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard] },
]),
],
declarations: [StoreTemplateComponent, DashboardComponent]
})
export class StoreModule { }

notfound.module.ts

@NgModule({
imports: [
CommonModule,
RouterModule.forChild([
{
path: '**',
component: NotfoundComponent
}
])
],
declarations: [ NotfoundComponent ],
})
export class NotfoundModule { }

最佳答案

您的路由器设置看起来没问题。要看的一件事是您在必须重定向的“仪表板”中有 AuthGuard那个重定向 URL 一定是错误的,所以你被重定向到 NotFoundComponent。

希望这能解决您的问题。

关于Angular 懒加载路由总是去通配符模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46619111/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com