Angular 路线不准确 URL

Angular routes not making exact URL

Angular 路由切换父级,当我期望给我一个 404 页面时可以直接转到子级。 这是应用模块的路径

{
 path: "mocha",
 loadChildren: () =>
  import("./navigation/navigation.module").then((m) => m.NavigationModule),
  
},
{
 path : "**",
 redirectTo : "error/404"
},

这是导航模块中的子路由

{
path : "admin",
loadChildren: () =>
  import("./admin/admin.module").then((m) => m.AdminModule),
component : AdminComponent,
},
{
  path : "emplyer",
  loadChildren: () =>
    import("./employer/employer.module").then((m) => m.EmployerModule),
  component : EmployerComponent
  },

2 个 URL:“/mocha/admin”和“/admin”给我相同的结果,但是,我正在等待在 /admin

中给出错误

https://codesandbox.io/s/vibrant-frog-vmqjf

尝试对导航模块中的子路由进行此更改:

 {
    path: '', // It will take the "base" route as defined in paerent route (/mocha/ in this case)
    children: [
       {
           path : "admin",
           loadChildren: () => import("./admin/admin.module").then((m) => m.AdminModule),
           component : AdminComponent,
       },
       {
           path : "emplyer",
           loadChildren: () =>
           import("./employer/employer.module").then((m) => m.EmployerModule),
           component : EmployerComponent
       },
...

你发布的代码中没有提到它,但你必须确保你没有直接在应用程序模块中使用管理模块,这可能是你有 /admin 工作的原因。

const appRoutes: Routes = [
  {
    path: "",
    redirectTo="error/404"
    pathMatch:full
  },
  {
    path: "mocha",
    component: MochaComponent,
    loadChildren: () =>
      import("./mocha/mocha.module").then((m) => m.MochaModule)
  }
];

问题出在导入上,我正在导入 navigation Module

imports: [
    NavigationModule, // this module need to be deleted !
    Template Module,
    MatButtonModule,
    LayoutModule,
    
    HttpClientModule,
    BrowserModule,
    BrowserAnimationsModule,
    RouterModule.forRoot(appRoutes),
  ],