简体   繁体   English

嵌套模块的 Angular 404 页面路由

[英]Angular 404 page routing for nested modules

I'm Using nested modules in my project我在我的项目中使用嵌套模块

.
└─ AppModule
    ├─ MallModule
    ├─ OtherModule
    └─ ...

In the main route I only configured top-level routes:在主路由中,我只配置了顶级路由:

app-routing.module.ts app-routing.module.ts

const routes: Routes = [
  { path: '',   redirectTo: '/', pathMatch: 'full' },
  { path: 'login', component: LoginComponent},
  { path: 'register', component: RegisterComponent },

  { path: '404', component: NotfoundComponent },
  { path: '**', redirectTo: '404' }, // Added
]

Separately, I configured routes separately in each sub-modules, like:另外,我在每个子模块中分别配置了路由,例如:

mall-routing.module.ts mall-routing.module.ts

const routes: Routes = [
  {
    path: '', 
    component: MallComponent,
    children: [
      {
        path: '',
        component: HomeComponent,
      },
      {
        path: 'about',
        component: AboutComponent,
      },
      ...
    }
]

The result is, because that no other routes are defined in the main routing configs, all requests other than login/register/404 will be redirected to 404.结果是,因为在主路由配置中没有定义其他路由,所以除了 login/register/404 之外的所有请求都将被重定向到 404。

Is there anyway I can use a correct 404 redirection but keep the current route file structure?无论如何我可以使用正确的 404 重定向但保留当前的路由文件结构吗? I don't hope to gather all route configs together.我不希望将所有路由配置收集在一起。

Thanks!谢谢!

import the 'Other' modules in your app modules, this will allow the routes defined in those modules to be used.在您的应用程序模块中导入“其他”模块,这将允许使用这些模块中定义的路由。

The updated code should look something like this:更新后的代码应如下所示:

imports: [
  MallModule,
  OtherModule
  RouterModule.forRoot([ // Add the configuration here, which is not a part of other module ])
]

in routing load your modules like below在路由中加载您的模块,如下所示

  // MallModule
  {
    path: "path",
    canLoad: [AuthGuard],
    loadChildren: "./modules/MallModule.module#MallModule",

  },

I arrived in at this question after one module worked fine with the routing but another gave 404 for all its subpages.在一个模块与路由一起工作正常但另一个模块为其所有子页面提供 404 之后,我到达了这个问题。

The problem for me was the order in app.modules.ts .我的问题是app.modules.ts中的顺序。 I had the submodules after the import of AppRoutingModule in the list of imports.在导入列表中导入AppRoutingModule后,我有子模块。 Once I moved that last, than the routing of submodules and 404 page worked fine.一旦我最后移动,比子模块和 404 页面的路由工作正常。

In app.module.ts.在 app.module.ts 中。

  imports: [
    BrowserModule,
    HttpClientModule,
    FormsModule,
    SubModule,
    AuthorizationModule,
    CommonModule,
    FontAwesomeModule,
    ProfileModule, // this was previously defined after AppRoutinModule
    AppRoutingModule // this must be last in list for routing with 404 to work
  ]

and in app-routing.modules.ts.并在 app-routing.modules.ts 中。

const routes: Routes = [
    // https://angular.io/guide/router
    { path: 'help', component: HelpComponent },
    { path: 'terms', component: TermsComponent },
    { path: 'contact', component: ContactComponent},
    { path: '404', component: PageNotFoundComponent},
    { path: '**', component: PageNotFoundComponent}
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM