Angular Routing Error: Cannot match any routes

Angular Routing Error: Cannot match any routes

我正在尝试在我的 angular 应用程序中实现延迟加载。我已将我的应用程序分为两个部分;认证模块和主页模块。我在 app-routing.Module.ts 中定义了我的路线,如下所示:

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

const routes: Routes = [
  { path: '', 
    redirectTo: '/authentication/signin', 
    pathMatch: 'full' 
  },
  {
    path: '',
    children: [
      {
        path: 'authentication',
        loadChildren: () => import('./modules/authentication/authentication.module').then(m => m.AuthenticationModule)
      },
      {
        path: 'home',
        loadChildren: () => import('./modules/home/home.module').then(m => m.HomeModule)
      }
    ]
  }
];

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

然后在身份验证和主页模块中,我又定义了一组路由,

authentication.module.ts:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { SignupComponent } from './signup/signup.component';
import { SigninComponent } from './signin/signin.component';
import { ForgotPasswordComponent } from './forgot-password/forgot-password.component';
import { SharedModule } from '../shared/shared.module';

export const authenticationRoutes = [
  {
    path: '',
    redirectTo: 'signup',
    pathMatch: 'full'
  },
  {
    path: 'signin',
    component: SigninComponent
  },
  {
    path: 'signup',
    component: SignupComponent
  },
  {
    path: 'forgot-password',
    component: ForgotPasswordComponent
  }
];

@NgModule({
  declarations: [
    SignupComponent,
    SigninComponent,
    ForgotPasswordComponent
  ],
  imports: [
    CommonModule,
    SharedModule,
    RouterModule.forChild(authenticationRoutes)
  ]
})
export class AuthenticationModule { }

home.module.ts:

import { HomeComponent } from './home/home.component';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

export const authenticationRoutes = [
  {
    path: 'home',
    component: HomeComponent
  }
];

@NgModule({
  declarations: [],
  imports: [
    CommonModule
  ]
})
export class HomeModule { }

有了这个,每当我尝试执行我的代码时,我希望一个空的 URL 将我重定向到注册,但是无论我是否指定路由,我最终都会得到同样的错误,一个状态:

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'authentication/signin' Error: Cannot match any routes. URL Segment: 'authentication/signin’

我该如何解决这个问题?

您的应用程序路由模块存在问题。在检查匹配路径时,两条路径都是空白的,并且由于您首先在路径为空白时重定向,因此应用程序永远不会到达任何其他路径。最好的方法是不要将身份验证和主模块指定为子模块,并在看到路径 'authentication' 时重新路由到身份验证,如下所示

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

const routes: Routes = [
    {
        path: 'authentication',
        loadChildren: () => import('./modules/authentication/authentication.module').then(m => m.AuthenticationModule)
    },
    {
        path: 'home',
        loadChildren: () => import('./modules/home/home.module').then(m => m.HomeModule)
    },
    { 
        path: '**', //If path doesn't match anything reroute to /authentication/signin
        redirectTo: '/authentication/signin', 
        pathMatch: 'full' 
    },
];

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