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

Angular 19中使用<a>元素时路由功能失效问题求助

我最近在做一个简单的Angular应用练手,结果卡在路由这块了,实在没办法来求助!我没有使用独立组件,项目里专门建了个app.routing.module,代码如下:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { MonthDetailComponent } from './containers/month-detail/month-detail.component';
import { BudgetEntryComponent } from './components/budget-entry/budget-entry.component';

const routes: Routes = [
  { path: 'month/:id', component: MonthDetailComponent },
  { path: 'budget-entry', component: BudgetEntryComponent },
  { path: '', redirectTo: '/month/current', pathMatch: 'full' }
];

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

我在组件模板里用普通的<a>标签做跳转,比如写<a href="/month/2024-09">查看九月预算</a>,结果点击之后完全没反应,要么就是直接触发页面刷新,甚至跳转到404页面,完全不是Angular单页应用路由该有的表现。

后来我试了用Angular自带的routerLink指令,写成<a routerLink="/month/2024-09">查看九月预算</a>是能正常跳转的,但我有些场景下需要用普通的<a>标签,或者动态生成的链接,怎么才能让Angular路由接管这些普通<a>的跳转行为呢?

我已经自己排查了几个点:

  • 确认AppRoutingModule已经正确导入到AppModule中
  • 反复检查了路由路径的拼写,没有错误
  • 试过在路由配置里加上useHash: true,但普通<a>还是不行

有没有大佬遇到过类似的问题,给我指个方向?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.08 12:43:07