Angular Router不会从基本路径重定向

时间:2019-12-05 12:48:07

标签: angular angular-ui-router

我正在尝试进行我的第一个多模块角度项目。我的主要应用路由如下:

export const AppRoutes: Routes = [
  {
    path: '',
    component: AppWithHeaderComponent,
    children: [
      {
        path: '',
        pathMatch: 'full',
        redirectTo: 'dashboard'
      },
      {
        path: 'dashboard',
        loadChildren: () => import('./dashboard/dashboard.module').then(m => m.DashboardModule)
      }

    ]
  },
  {
    path: '',
    component: AppBlankComponent,
    children: [
      {
        path: 'authentication',
        loadChildren: () => import('./authentication/authentication.module').then(m => m.AuthenticationModule)
      }
    ]
  }
];

我希望如果我打开基本路径(localhost:4200),它将重定向到/ dashboard,但这不会发生。我只看到主要app.component的黑屏,控制台日志中没有错误。

我将其范围缩小了,所以单独的模块不会混淆任何东西:

{
    path: '',
    pathMatch: 'full',
    redirectTo: 'dashboard'
  },
  {
    path: 'dashboard',
    component:DashboardComponent
  }

,但它仍然不执行任何操作。 在这两种情况下,如果我手动输入/ dashboard,都可以正确打开组件。

我也尝试过调试

RouterModule.forRoot(AppRoutes,{ enableTracing: true })

但是我看到的唯一有用的是URL确实没有被重定向:

url: "/"
urlAfterRedirects: "/"

我已经用尽了所有想法,因为这在另一个项目中可以完美地工作,并且在这里也应如此。 任何帮助或线索表示赞赏! :)

更新:

好,所以问题在于我的AuthenticationModule(包含带有路由的登录组件)也被导入到app.module中,因为我想使用AuthGuard,它也放置在AuthenticationModule中。

身份验证模块的路由如下:

{
    path: '',
    children: [
      {
        path: 'login',
        component: LoginComponent
      }
    ]
  }

现在,此空路径正在覆盖app.module的空路径路由,因此这就是为什么它不起作用的原因。

现在,我已经将auth.guard从AuthenticationModule中移出了,所以现在可以了,但是有没有办法像这样使用它? (app.module中的导入顺序未做任何更改)

0 个答案:

没有答案