You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular路由报错:Cannot match any routes. URL Segment: 'auth/login' 求助

解决Angular路由「Cannot match any routes. URL Segment: 'auth/login'」问题

问题根源

  1. 根路由中Auth模块的路径配置错误,导致/auth路径无法匹配到对应模块
  2. Auth模块内未设置父路由指向AuthComponent,子路由(login/register等)没有正确的挂载容器,同时AuthComponent也不会被渲染

具体修复步骤

1. 修改app-routing.module.ts

将Auth模块的路由路径从空字符串改为auth,确保/auth开头的请求能正确加载Auth模块:

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

const routes: Routes = [
  {
    path: 'auth', // 把空路径改为auth
    loadChildren: () => import('./web/auth/auth.module').then(m => m.AuthModule)
  },
  {
    path: 'account',
    loadChildren: () => import('./web/account/account.module').then(m => m.AccountModule)
  },
  { path: '**', redirectTo: 'auth/login' } // 可选:添加全局通配符路由,跳转默认登录页
];

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

2. 修改auth-routing.module.ts

添加父路由指向AuthComponent,并将所有子路由(login/register等)放入children数组中,这样子组件会渲染在AuthComponent的<router-outlet>内:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AuthComponent } from './auth.component'; // 导入AuthComponent
import { LoginComponent } from './login/login.component';
import { RegisterComponent } from './register/register.component';
import { ForgotPasswordComponent } from './forgot-password/forgot-password.component';
import { ResetPasswordComponent } from './reset-password/reset-password.component';

const routes: Routes = [
  {
    path: '', // 空路径对应/auth路径,挂载AuthComponent
    component: AuthComponent,
    children: [ // 子路由会渲染在AuthComponent的router-outlet中
      { path: 'login', component: LoginComponent },
      { path: 'register', component: RegisterComponent },
      { path: 'forgot-password', component: ForgotPasswordComponent },
      { path: 'reset-password', component: ResetPasswordComponent },
      { path: '', redirectTo: 'login', pathMatch: 'full' } // 访问/auth时默认跳转login
    ]
  }
];

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

3. 确认auth.module.ts配置

确保AuthComponent已被声明在模块中:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AuthRoutingModule } from './auth-routing.module';
import { AuthComponent } from './auth.component'; // 导入AuthComponent
import { LoginComponent } from './login/login.component';
import { RegisterComponent } from './register/register.component';
import { ForgotPasswordComponent } from './forgot-password/forgot-password.component';
import { ResetPasswordComponent } from './reset-password/reset-password.component';

@NgModule({
  declarations: [
    AuthComponent, // 声明AuthComponent
    LoginComponent,
    RegisterComponent,
    ForgotPasswordComponent,
    ResetPasswordComponent
  ],
  imports: [
    CommonModule,
    AuthRoutingModule
  ]
})
export class AuthModule { }

验证修复

完成上述修改后,访问/auth/login:

  • 页面会显示app component works!(来自AppComponent)
  • 同时显示auth works!(来自AuthComponent)
  • 下方渲染LoginComponent的内容

内容的提问来源于stack exchange,提问作者Aleix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 08:01:58