【问题标题】:Children routing not working and application redirects to the 404 page子路由不起作用,应用程序重定向到 404 页面
【发布时间】:2019-05-11 04:14:05
【问题描述】:

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

这是我的代码:

AppModule

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 { }

应用路由模块:

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 { }

仪表板模块文件:

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 { }

仪表板路由模块。

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 { }

问题是我想路由到 localhost:4200 上的仪表板布局,但它总是会在每条路由上转到 pageNotFound 组件路由。谁能指出是什么问题。

StackBlitz 上的代码:

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

所需的输出

【问题讨论】:

  • 您在仪表板模块之前添加了批准,因此 404 路由将首先命中,因此导航到未找到页面。你需要改变你的导入顺序。重要的是你导入的顺序。
  • @ChrisEenberg ,它工作。你能再解释一下,以便我能完全理解吗?
  • @ChrisEenberg 你能把它添加为这个问题的答案吗?

标签: angular routes angular-routing nested-routes


【解决方案1】:

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

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

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

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

routing 的文档中有一个非常好的部分。

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

克里斯的问候

【讨论】:

    猜你喜欢
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    相关资源
    最近更新 更多