如何路由到Angular2中的子组件?

时间:2017-08-22 07:27:21

标签: angular angular-routing

我正在尝试为项目进行基础设置。以下是我创建的文件:

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>

以下是 app.routes.ts 文件

中的路线
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>      

我可以登录并导航到主要组件页面,其中包含顶部栏,侧栏和内容部分,其中它将显示侧栏组件的内容,如仪表板,用户或报告。

但是,当我尝试单击侧栏组件(仪表板,用户或报告)时,组件不会显示在屏幕的右侧。我能得到任何帮助吗?

2 个答案:

答案 0 :(得分:3)

您好请从子路线配置中删除main /:

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

其余的代码都没问题。

答案 1 :(得分:1)

变化:

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

要:

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

否则您的路线需要看起来像 / main / main / dashboard 等。