未捕获(承诺):错误:无法匹配任何路由.URL 段:角路由、错误、URL

2023-09-06 08:50:10 作者:今日泪对镜

I am new to Angular. I am using Angular 7 and doing a simple routing. After Login page, I want to display a home page. Login is a part of app-root component and in home page I am showing header and sidenav but I am not able to route to home page.

app-routimg.module.ts

const appRoutes: Routes = [
  {
    path: '',
    loadChildren: './login/login.module#LoginModule'
},
  {
    path: 'dashboard',
    loadChildren: './dashboard/dashboard.module#DashBoardModule',

}
];
  @NgModule({
    imports: [RouterModule.forRoot(appRoutes)],
    exports: [RouterModule],
   // providers: [AuthGuard]
}
端口 IP地址过滤 路由器ip拜候的方式调集 路由器的ip地址

dashboard-routing.modul.ts

const appRoutes: Routes = [
  {
    path: 'dashboard',
    component: DashBoardComponent ,
    children: [
         {
             path: '',
            redirectTo: 'home'
        },
        {
            path: 'home',
            loadChildren: './home/home.module#HomeModule'
        },

    ]
}
];
@NgModule({
  imports: [
    RouterModule.forChild(appRoutes)
  ],
  exports: [
    RouterModule
  ]
})

login.component.ts

onSubmit() {

       this._router.navigate(['/home']);
      }
}

core.js:15724 ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'home' Error: Cannot match any routes. URL Segment: 'home'

解决方案

it should be like this in your dashboard-routing-module.ts:

const routes: Routes = [{
  path: '',
  component: DashBoardComponent,
  children: [
   {
      path: '',
      redirectTo: 'home',
      pathMatch: 'prefix'
   },
   {
      path: 'home',
      component: HomeComponent
   }
 ]
}];