0

I want to define multiple level route in angular 6, this is what I need:

for first level routes, I have:

dashboardRoutes:Routes=[
    {
        path: "dashboard",
        component: DashobardComponent,
        canActivate: [AuthGuard],
        children:[
            {
                path: 'user', component: UserComponent,
            },
            {
                path: 'overview', component:OverViewComponent
            }
        ]
    }
];

But now, I want to have overview to have their own child, so I defined html as:

<div>This is overvew</div>
<router-outlet></router-outlet>

And then I defined overview routes like below:

    const overviewRoutes:Routes = [
    {
        path: "",
        component: OverViewComponent,
        children:[
            {
                path:'reg',
                component: RegularComponent
            },
            {
                path:'rt',
                component: RealTimeComponent
            }
        ]
    }
];

then I got error:

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

Now, I am lost, overView is already defined in the second route, how should I update first route here?

I changed first one to

path: 'overview', redirectTo:'overview/reg', totally failed.

I could define all the routes in the first route definition, but that is not elegant enough at all. I want the overview route definition in its own module here.

Hope you understand what I mean here.

Thanks

3 Answers 3

4

Try this one:

const overviewRoutes:Routes = [
{
    path: "dashboard/overview", // <-- missed this
    component: OverViewComponent,
    children:[
        {
            path:'reg',
            component: RegularComponent
        },
        {
            path:'rt',
            component: RealTimeComponent
        }
    ]
}

];

Anguar says it cannot find the route dashboard/overview/reg, because you didn't define where is dashboard/overview. Simply put it to path of the first component.

Sign up to request clarification or add additional context in comments.

Comments

0

Simple, just have an empty path for the OverViewComponent:

const overviewRoutes:Routes = [
    {
        path: "",
        component: OverViewComponent,
        children:[
            {
                path:'reg',
                component: RegularComponent
            },
            {
                path:'anotherPage',
                component: anotherPageComponent
            }
        ]
    }
];

Comments

0

agree with Chrillewoodz's solution. Also, it is good to put 'review' component and it's children into a feature module. In there you can define route file for this feature module.

see official Angular document here, and also Deborah Kurata's Angular router topic talk in this year's NgConf

2 Comments

I tested using @chrillewoodz solution, not working, please see my updated contents
if you've already defined a review module and its own route file, make sure you register the route using 'forChild' command in the module file: imports: [RouterModule.forChild(routes)],

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.