2017-09-11 8 views
1

Angular 4プロジェクトでangular-tree-componentを使用しています。ノードをクリックすると、ホームページにコンポーネントを表示したい。代わりに、ノードをクリックするとノードコンポーネントが表示されますが、ホームページの要素はなくなります。クリックするとツリーノードごとに異なるコンポーネントを表示する方法は?

これは、ツリーノードを表示するホームページコンポーネントで私のコードです:

<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> 
+0

ホームコンポーネントはですか? – Vega

+0

これはホームコンポーネントの一部です。はホームコンポーネントのセレクタではありません。 – eli

答えて

1

あなたが例を次のように二<router-outlet>を使用する必要があります。

@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 { 
} 

とルートを定義します。

{ 
    path: '', 
    redirectTo: '/dashboard/users', 
    pathMatch: 'full' 
    }, 
    { 


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

DEMO

+0

私は理解できません。どうしてすべてのコンポーネントクラスを 'app.component.ts'の中に入れたのですか? – eli

+0

これは一例です。そして、はい、あなたはそれを行うことができます、本当のプロジェクトのためにalltoughtそれを避けるために良いです。 – Vega

+0

しかし、それは私の答えのポイントではありません。要点は、補助的なルートが必要であるということです。 – Vega

関連する問題