Angular Routing Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'dashboard/profile'

Angular Routing Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'dashboard/profile'

自从我开始在我的 angular 项目中使用延迟加载模块后,我突然收到 错误:无法匹配任何路由

所以现在我有两个模块

TeacherDashboardModule 中,我有以下组件:

Dashboard and Profile are 2 seperate pages where top-nav sits above on both of them.

TopNavComponent 有一个下拉菜单,如下所示

<ul class="navbar-nav ml-auto mt-2 mt-lg-0 navbar-right">
    <li class="nav-item dropdown">
      <a class="nav-link" class="dropdown-toggle" data-toggle="dropdown">
        <img src="{{user.photoUrl}}" />
        <strong>{{user.firstName}}</strong>
        <span class="glyphicon glyphicon-chevron-down"></span>
      </a>
      <ul class="dropdown-menu">
        <li> <a routerLink = "profile">Profile</a> </li>
        <li> <a routerLink="settings">Settings</a> </li>
        <li> <a routerLink="dashboard"> Dashboard </a> </li>
        <li> <a routerLink = "blog">Blog</a> </li>            
        <li> <a href="javascript:void(0)">Privacy Policy</a> </li>
        <li> <a href = "javascript:void(0)" (click) = "logout()">Logout</a> </li>
      </ul>
    </li>
  </ul>

所以当我点击 link 例如 profile 时,我得到这个错误:

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'dashboard/profile' Error: Cannot match any routes. URL Segment: 'dashboard/profile'

问题是我打算将仪表板和个人资料页面分开,所以当我单击 dashboard 时,它应该转到 localhost:4200/dashboard 并且当我单击 profile 它应该去 localhost:4200/profile

这是我在 teacher-dashboard-routing.module.ts 中的路线:

const routes: Routes = [


  {
    path: '',
    redirectTo: '/dashboard',
    pathMatch: 'full'
  },
  {
    path: 'profile',
    component: ProfileComponent
  },
  {
    path: 'dashboard',
    component: DashboardComponent
  }



];

我明白发生了什么,但我不确定如何解决这个问题。我可以让它采用绝对路径而不是相对路径吗?

您可以使用相对路径,如下所示:<li> <a routerLink = "../profile">Profile</a> </li>