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

Angular 17路由报错NG04002:无法匹配路由问题求助

Angular 17路由跳转报错NG04002的解决方法

问题根源

你遇到的Error: NG04002: Cannot match any routes. URL Segment: 'Cart'是因为路由路径大小写不匹配:

  • 模板中routerLink使用的是大写开头的/Cart
  • 路由配置里定义的路径是全小写的cart
    Angular路由默认是大小写敏感的,因此无法匹配到对应的路由规则。

解决方法

方法1:统一路径大小写(推荐)

修改App.Component.htm中的routerLink,将大写的Cart改为小写的cart:

<div>
    <a routerLink="/cart">Cart</a>
</div>

方法2:关闭路由大小写敏感(可选)

如果需要保留大写的URL路径,可以在路由配置中关闭大小写敏感。修改App.Routes.ts的RouterModule.forRoot配置:

@NgModule({
  imports: [RouterModule.forRoot(routes, {
    caseSensitive: false // 禁用路由路径大小写敏感
  })],
  exports: [RouterModule]
})
export class AppRoutingModule{

}

额外检查(Angular 17独立组件模式)

由于你的AppComponent是独立组件(standalone: true),需确保路由配置已正确注入:
如果使用Angular 17推荐的独立API,main.ts应包含路由提供器:

import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { provideRouter } from '@angular/router';
import { routes } from './app/app.routes';

bootstrapApplication(AppComponent, {
  providers: [
    provideRouter(routes)
  ]
});

如果仍使用NgModule模式,确保根模块AppModule已导入AppRoutingModule。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 04:48:32