儿童路由不工作,应用程序重定向到 404 页面

Ahs*_*san 3 routes nested-routes angular-routing angular

我创建了一个 Angular 应用程序版本 7,项目结构是这样的。app 模块有 app-routing.module.ts,dashboard 模块有dashboard-routing module.ts。在仪表板模块中,布局组件路由有子组件,即 home 和 admin。

这是我的代码:

应用模块

import { AppRoutingModule } from './app-routing.module';
import { DashboardModule } from './dashboard/dashboard.module';

import { AppComponent } from './app.component';
import { LoginComponent } from './login/login.component';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';


@NgModule({
  declarations: [
    AppComponent,
    LoginComponent,
    PageNotFoundComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    DashboardModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

应用路由模块:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LoginComponent } from './login/login.component';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';

const routes: Routes = [
  { path: 'login', component: LoginComponent },
  { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
  { path: '**', component: PageNotFoundComponent }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }
Run Code Online (Sandbox Code Playgroud)

仪表板模块文件:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LayoutComponent } from './layout/layout.component';
import { HomeComponent } from './home/home.component';
import { AdminComponent } from './admin/admin.component';
import { DashboardRoutingModule } from './dashboard-routing.module';

@NgModule({
  declarations: [LayoutComponent, HomeComponent, AdminComponent],
  imports: [
    CommonModule,
    DashboardRoutingModule
  ]
})
export class DashboardModule { }
Run Code Online (Sandbox Code Playgroud)

仪表板路由模块。

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Routes, RouterModule } from '@angular/router';

import { LayoutComponent } from './layout/layout.component';
import { HomeComponent } from './home/home.component';
import { AdminComponent } from './admin/admin.component';


const dashboardRoutes: Routes = [
  {
    path: 'dashboard',
    component: LayoutComponent,
    children: [
      { path: '', redirectTo: 'home', pathMatch: 'full' },
      { path: 'home', component: HomeComponent },
      { path: 'admin', component: AdminComponent}
    ]
  }
];

@NgModule({
  declarations: [],
  imports: [
    CommonModule,
    RouterModule.forChild(dashboardRoutes)
  ],
  exports: [RouterModule]
})
export class DashboardRoutingModule { }
Run Code Online (Sandbox Code Playgroud)

问题是我想路由到 localhost:4200 上的仪表板布局,但它总是在每条路由上转到 pageNotFound 组件路由。任何人都可以指出是什么问题。

StackBlitz 上的代码:

https://stackblitz.com/github/Ahsan9981/authGuardApp

期望输出 在此处输入图片说明

Chr*_*erg 6

路由器将按照您导入的顺序匹配您的路由,因此,如果您添加通配符 /** 以匹配 appModule 开头的所有路由,它将在检查您的任何其他路由是否匹配之前匹配此路由.

扩展示例 - 路由器将运行所有定义的路由,直到找到匹配项,如果您从全部捕获开始,它将停止并不再继续。因此,在模块的开头(appRoutes)定义一个catch all 将是一个问题。

您的仪表板模块会导入您的仪表板路由,因此在应用模块中导入仪表板模块将决定何时导入仪表板路由。

您需要按照您认为会匹配的顺序导入您的路由,并在最后添加通配符。这应该始终是使用通配符的后备。

路由文档中对此有一个很好的部分。

如果还有不清楚的地方,请随时发表评论,我会相应地更新我的答案。

亲切的问候克里斯