首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在单击每个树节点时显示不同的组件?

如何在单击每个树节点时显示不同的组件?
EN

Stack Overflow用户
提问于 2017-09-11 09:29:49
回答 1查看 482关注 0票数 1

我正在使用angular-tree-component在一个角4项目。单击节点时,我希望在主页中显示组件。相反,当我单击一个节点时,它会显示节点组件,但是主页元素会消失。

这是我在主页组件中显示树节点的代码:

代码语言:javascript
复制
  <div class="Tree">
<tree-root [nodes]="nodes">
  <ng-template #treeNodeTemplate let-node let-index="index">
<a routerLink="{{node.data.name}}" routerLinkActive="active" ><span>{{ 
node.data.name }}</span></a>
  </ng-template>
</tree-root>
</div>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-11 10:53:47

您需要使用第二个<router-outlet>,如下例所示:

代码语言:javascript
复制
@Component({
  selector: 'my-app',
  template: `
    <router-outlet></router-outlet>
  `
})
export class AppComponent {
}


@Component({
  template: `
    <h3 class="title">Dashboard</h3>
    <nav>
      <a routerLink="users" routerLinkActive="active" >Users</a>
      <a routerLink="profile" routerLinkActive="active" >Profile</a>
    </nav>
    <hr />
    <router-outlet></router-outlet>
  `
})
export class DashboardComponent {
}

@Component({
  template: `
    <h3 class="title">Profile</h3>
  `
})
export class ProfileComponent {
}

@Component({
  template: `
    <h3 class="title">Users</h3>
  `
})
export class UsersComponent {
}

并确定路线:

代码语言:javascript
复制
 {
    path: '',
    redirectTo: '/dashboard/users',
    pathMatch: 'full'
  },
  {


  path : 'dashboard',
  component: DashboardComponent,
  children:[
      {
       path : 'users',
       component: UsersComponent
      },
      {
       path : 'profile',
       component: ProfileComponent
      }
   ]
  }

演示

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46152355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档