Angular 路由 redirectTo 显示错误的组件
Angular routing redirectTo displays wrong component
我知道这个问题已经有多个答案,但其中 none 对我有帮助。我的问题是路由器重定向没有重定向到正确的页面,我找不到原因。我的设置是:
app-routing.module.ts
export const routes: Routes = [
{ path: '', redirectTo: 'start', pathMatch: 'full' },
{ path: 'start', component: StartComponent, canActivate: [UManagementGuard] },
{ path: 'history', loadChildren: () => import('./history/history.module').then(m => m.HistoryModule), canActivate: [UManagementGuard]
},
{ path: 'preview', component: PreviewComponent, canActivate: [UManagementGuard] },
{ path: 'live', loadChildren: () => import('./live/live.module').then(m => m.LiveModule),
canActivate: [UManagementGuard] },
{ path: 'p-help', component: PHelpComponent },
{ path: '**', redirectTo: '/lotList', pathMatch: 'full'}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
所以当我 运行 应用程序开始时它工作正常:
http://localhost:4200/start
但是当我调用 http://localhost:4200 或 http://localhost:4200/
它显示 PreviewComponent 但不显示 StartComponent???
我已经试过了:
{ path: '', redirectTo: '/start', pathMatch: 'full' }
or
{ path: '', component: StartComponent }
知道为什么会发生这种情况吗?
终于找到bug了
我的 PreviewComponent 是一个延迟加载的模块,我更改了它。但是我忘了删除 PreviewRoutingModule,它仍然在将任何空路由重定向到它自己的启动组件时触发。
我知道这个问题已经有多个答案,但其中 none 对我有帮助。我的问题是路由器重定向没有重定向到正确的页面,我找不到原因。我的设置是:
app-routing.module.ts
export const routes: Routes = [
{ path: '', redirectTo: 'start', pathMatch: 'full' },
{ path: 'start', component: StartComponent, canActivate: [UManagementGuard] },
{ path: 'history', loadChildren: () => import('./history/history.module').then(m => m.HistoryModule), canActivate: [UManagementGuard]
},
{ path: 'preview', component: PreviewComponent, canActivate: [UManagementGuard] },
{ path: 'live', loadChildren: () => import('./live/live.module').then(m => m.LiveModule),
canActivate: [UManagementGuard] },
{ path: 'p-help', component: PHelpComponent },
{ path: '**', redirectTo: '/lotList', pathMatch: 'full'}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
所以当我 运行 应用程序开始时它工作正常: http://localhost:4200/start
但是当我调用 http://localhost:4200 或 http://localhost:4200/ 它显示 PreviewComponent 但不显示 StartComponent???
我已经试过了:
{ path: '', redirectTo: '/start', pathMatch: 'full' }
or
{ path: '', component: StartComponent }
知道为什么会发生这种情况吗?
终于找到bug了
我的 PreviewComponent 是一个延迟加载的模块,我更改了它。但是我忘了删除 PreviewRoutingModule,它仍然在将任何空路由重定向到它自己的启动组件时触发。