Angular routerLink 路由正确但 route.navigate 在组件路由到默认路径

Angular routerLink routes properly but route.navigate in component routes to default path

基本示例已创建
源代码是 Uploaded Here Deployed Here

这是基本的但很奇怪,我哪里错了

它在第 19 行的 app.component.html 上被重定向:

<a class="nav-link" href="#" (click)="navigateBack()"> Products(navigated in Component)</a>

此标签有 href="#",因此页面被路由到您的根目录,因为您的 app-routing.module.ts 文件正在重定向到主页。

  { path: '', redirectTo: 'home', pathMatch: 'full' },
  { path: '**', redirectTo: 'home', pathMatch: 'full' }

因此,删除 href="#"[routerLink]="[]" 应该可以解决问题。