angular2 routing problem

angular2 routing problem

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

The handling of this problem is very simple, the route path is wrong,

import {NgModule} from'@angular/core';
import {Routes, RouterModule} from'@angular/router';
import {loginComponent} from'./login/login.component';
import{indexComponent} from "./index/index.component";
import {mainComponent} from "./main/main.component";
const routes: Routes = [
 {path:'login',component:loginComponent},
 {path:'index',component:indexComponent,children:[
   {path:"main",component:mainComponent},
   {path:'',redirectTo:'/main',pathMatch:'full'}
   ]},
 {path:'',redirectTo:'/index',pathMatch:'full'}
];

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

In my case, this problem occurred when the child route was called in the parent component. You only need to change one place.

{path:'',redirectTo:'main',pathMatch:'full'}

Just remove the slash before main. I think the reason for the problem is that the sub-route is called, so there is no need for a slash, which is embarrassing!

Anyway, the problem is solved

Reference: https://cloud.tencent.com/developer/article/1148224 angular2 routing problem-cloud + community-Tencent Cloud