Angular 8 默认路由

bjw*_*hip 5 components routes angular angular8

我有一个运行 Angular 8.2 的应用程序。除默认路由外,路由运行良好。

当我去时website.com我想被重定向到website.com/sign-in

应用程序路由模块

const routes: Routes = [
  {
    path: '',
    redirectTo: 'sign-in',
    pathMatch: 'full'
  },
  {
    path: '',
    component: PublicLayoutComponent,
    data: { title: 'Public Views' },
    children: PUBLIC_ROUTES
  },
  {
    path: '',
    component: PrivateLayoutComponent,
    data: { title: 'Private Views' },
    children: PRIVATE_ROUTES
  },
];
Run Code Online (Sandbox Code Playgroud)

公共路由模块

export const PUBLIC_ROUTES: Routes = [
  {
    path: "sign-in",
    loadChildren: () =>
      import('../features/sign-in/sign-in.module').then(
        m => m.SignInModule
      )
  }
];
Run Code Online (Sandbox Code Playgroud)

私有路由模块

export const PRIVATE_ROUTES: Routes = [
  {
    path: "dashboard",
    loadChildren: () =>
      import('../features/dashboard/dashboard.module').then(
        m => m.DashboardModule
      )
  }
];
Run Code Online (Sandbox Code Playgroud)

我也尝试将其添加到app-routing.module

{
  path: '**',
  redirectTo: 'sign-in',
  pathMatch: 'full'
},
Run Code Online (Sandbox Code Playgroud)

编辑:

我所有的路线都有效。当转到 时website.com/sign-in,Angular 会查看app-routing.module,然后查看子路由以查找路由位置。如果我添加

{
  path: '**',
  redirectTo: 'sign-in',
  pathMatch: 'full'
}
Run Code Online (Sandbox Code Playgroud)

对于public-routing.module,除空白之外的任何内容都将重定向到登录。website.com/jdjhjdfjd将进入我的登录页面,但website.com不会。

小智 -1

您应该检查如何使用 Route Guards 或者您可以使用 Router 来重定向它:

export class AppComponent implements OnInit { 
  constructor(private router: Router) {}

  ngOnInit() {
    this.router.navigate(['sign-in'])
  }
}
Run Code Online (Sandbox Code Playgroud)