简体   繁体   English

角度通配符路由替换子路由

[英]Angular wildcard route replacing child routes

I have a module called "host" with its own routing that I want to insert into the app-routing.module. 我有一个名为“host”的模块,它有自己的路由,我想插入app-routing.module。 However, I have the problem of the wildcard loading first and displaying PageNotFoundComponent, instead of the Host component loading. 但是,我首先遇到通配符加载问题并显示PageNotFoundComponent,而不是主机组件加载。 I have the following files. 我有以下文件。

host.module.ts host.module.ts

....
const routes: Routes = [
  {
    path: 'host',
    children: [
     { path: '', component: HostComponent }
    ]
  }
];

@NgModule({
  declarations: [HostComponent],
  imports: [
    CommonModule,
    RouterModule.forChild(routes)
  ]
})
export class HostModule { }

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

const routes: Routes = [
  { path: '', component: HomeComponent, pathMatch: "full"},
  { path: '**', component: PageNotFoundComponent }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

app.module.ts app.module.ts

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    PageNotFoundComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HostModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.html app.component.html

<h2>Home</h2>
<ul>
  <li>
    <h2><a routerLink="/host">host</a></h2>
  </li>
</ul>
<router-outlet></router-outlet>

Problem: When I run the app and click on the "Host" button, it loads the PageNotFoundComponent. 问题:当我运行应用程序并单击“主机”按钮时,它会加载PageNotFoundComponent。 I obviously want it to go to the HostComponent. 我显然希望它转到HostComponent。

在此输入图像描述

In your app.module.ts you need to reorder your imports app.module.ts您需要重新排序导入

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    PageNotFoundComponent
  ],
  imports: [
    BrowserModule,
    HostModule, <--- this before AppRoutingModule
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Reason being is because the order of the routes in the configuration matters. 原因是因为配置中的路由顺序很重要。 https://angular.io/guide/router#configuration https://angular.io/guide/router#configuration

The ** path in the last route is a wildcard. 最后一条路径中的**路径是通配符。 The router will select this route if the requested URL doesn't match any paths for routes defined earlier in the configuration. 如果请求的URL与配置中先前定义的路由的任何路径不匹配,路由器将选择此路由。 This is useful for displaying a "404 - Not Found" page or redirecting to another route. 这对于显示“404 - 未找到”页面或重定向到另一个路径非常有用。

The order of the routes in the configuration matters and this is by design. 配置中的路由顺序很重要,这是设计的。 The router uses a first-match wins strategy when matching routes, so more specific routes should be placed above less specific routes. 路由器在匹配路由时使用第一匹配胜利策略,因此应将更具体的路由放置在不太具体的路由之上。 In the configuration above, routes with a static path are listed first, followed by an empty path route, that matches the default route. 在上面的配置中,首先列出具有静态路径的路由,然后列出与默认路由匹配的空路径路由。 The wildcard route comes last because it matches every URL and should be selected only if no other routes are matched first. 通配符路由是最后一个,因为它匹配每个URL,只有在没有其他路由首先匹配时才应选择。

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

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