【问题标题】:Angular routing redirectTo displays wrong componentAngular 路由 redirectTo 显示错误的组件
【发布时间】:2021-11-05 21:48:34
【问题描述】:

我知道这个问题已经有几个答案,但没有一个对我有帮助。我的问题是路由器重定向没有重定向到正确的页面,我不知道为什么。我的设置是:

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:4200http://localhost:4200/ 它显示 PreviewComponent 但不显示 StartComponent???

我已经试过了:

{ path: '', redirectTo: '/start', pathMatch: 'full' }
or
{ path: '', component: StartComponent }

任何想法为什么会发生这种情况?

【问题讨论】:

  • 您可能想检查UManagementGuard 正在做什么,可能那里发生了另一个重定向。
  • 我禁用了 Guard,但仍然出现同样的错误

标签: angular redirect routes


【解决方案1】:

我终于找到了错误。

我的 PreviewComponent 是一个延迟加载的模块,我对其进行了更改。但是我忘了删除 PreviewRoutingModule,它仍然在重定向任何空 Route 到它自己的启动组件中触发。

【讨论】:

    猜你喜欢
    • 2017-10-21
    • 2019-09-15
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 2018-08-03
    • 2017-06-04
    • 2018-10-02
    相关资源
    最近更新 更多