Uncaught (in promise): Error: Cannot match any routes. URL Segment: Angular

穿精又带淫゛_ 提交于 2021-02-05 10:50:06

问题


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]
}

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'


回答1:


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
   }
 ]
}];



回答2:


you must use this code :

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

if it doesn't work, in "dashboard-routing.modul.ts" file use path: ' ', instead of
path: 'dashboard',.



来源:https://stackoverflow.com/questions/56088453/uncaught-in-promise-error-cannot-match-any-routes-url-segment-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!