Raghav
Raghav

Reputation: 1229

How to route to the child components in Angular2?

I am trying to do the base setup for a project. Below are the files I created:

app.component.html

<div class="jumbotron">
    <div class="container">
        <div class="col-sm-8 col-sm-offset-2">
            <router-outlet></router-outlet>
        </div>
    </div>
</div>

Below is my routes in app.routes.ts file

const appRoutes: Routes = [
    { path: 'login', component: LoginComponent },
    {
      path: 'main',
      component: MainComponent,
      children: [
        {
            path: 'main/dashboard',component: DashboardComponent
        },
        {
            path: 'main/user',component: UserComponent
        },
        {
            path: 'main/reports',component: ReportsComponent
        }
      ]
    },

    { path: '**', redirectTo: 'main' }
];

main.component.html

<div id="wrapper">
  <app-top-bar></app-top-bar>
  <router-outlet></router-outlet>
</div>

topbar.component.html

<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="navbar-header">
        <a class="navbar-brand">My Admin</a>
    </div>
    <ul class="nav navbar-right top-nav">
        <li>
            <a [routerLink]="['/login']"><i class="fa fa-fw fa-user"></i> Logout</a>
        </li>
    </ul>
  <app-side-bar></app-side-bar>
</nav>  

sidebar.component.html

<div class="collapse navbar-collapse navbar-ex1-collapse">
  <ul class="nav navbar-nav side-nav">
    <li [routerLink]="['main/dashboard']" routerLinkActive="active">
        <a><i class="fa fa-fw fa-dashboard"></i> Dashboard</a>
    </li>
    <li [routerLink]="['main/user']" routerLinkActive="active">
        <a ><i class="fa fa-fw fa-bar-chart-o"></i> Users</a>
    </li>
    <li [routerLink]="['main/reports']" routerLinkActive="active">
        <a><i class="fa fa-fw fa-table"></i> Reports</a>
    </li>
  </ul>
</div>      

I am able to login and navigate to the main component page, which contains a topbar, a sidebar and a content section wherein it will display the content of the sidebar components, like dashboard, users, or reports.

But, when I try to click the sidebar components (dashboard, users, or reports), the components are not displaying in right part of the screen. Can I get any help?

Upvotes: 0

Views: 4966

Answers (2)

user1608841
user1608841

Reputation: 2475

Hi Please remove main/ from your child route configuration:

const appRoutes: Routes = [
    { path: 'login', component: LoginComponent },
    {
      path: 'main',
      component: MainComponent,
      children: [
        {
            path: 'dashboard',component: DashboardComponent
        },
        {
            path: 'user',component: UserComponent
        },
        {
            path: 'reports',component: ReportsComponent
        }
      ]
    },

    { path: '**', redirectTo: 'main' }
];

and rest of your code is fine.

Upvotes: 4

Fredrik Lundin Grande
Fredrik Lundin Grande

Reputation: 8186

Change:

children: [
        {
            path: 'main/dashboard', component: DashboardComponent
        },
        {
            path: 'main/user', component: UserComponent
        },
        {
            path: 'main/reports', component: ReportsComponent
        }
      ]

To:

children: [
        {
            path: 'dashboard', component: DashboardComponent
        },
        {
            path: 'user', component: UserComponent
        },
        {
            path: 'reports', component: ReportsComponent
        }
      ]

Otherwise your routes will need look like /main/main/dashboard etc to hit.

Upvotes: 1

Related Questions