2017-09-25 83 views
2

我,安裝有進口角樹組件,並試圖將其設置使用下面的步驟提供了基本的例子https://angular2-tree.readme.io/角Tree組件無法正常工作

但不幸的是,我只看到了根節點和沒有擴大。如果發生任何錯誤,發佈代碼可以讓人看到蝙蝠嗎?請幫我理解我顯然無法看到的錯誤。

模塊:

import { NgModule } from '@angular/core'; 
import { TreeModule } from 'angular-tree-component'; 

@NgModule({ 
    imports: [ 
    TreeModule 
    ], 
    declarations: [], 
    providers: [] 
}) 
export class CourseCreationModule { } 

組件:

import { Component, OnInit } from '@angular/core'; 
import { Router, ActivatedRoute, Params } from '@angular/router'; 

@Component({ 
    selector: 'app-container', 
    templateUrl: './container.component.html', 
    styleUrls: ['./container.component.css'] 
}) 
export class ContainerComponent implements OnInit { 
    tree: any; 

    constructor() { } 

    getCourseDetails() { 
     this.createLessonTree(); 
    } 

    createLessonTree() { 
    this.tree = [ 
     { 
     id: 1, 
     name: 'root1', 
     children: [ 
      { 
      id: 2, 
      name: 'child1' 
      }, 
      { id: 3, 
      name: 'child2' 
      } 
     ] 
     }, 
     { 
     id: 4, 
     name: 'root2', 
     children: [ 
      { id: 5, name: 'child2.1' }, 
      { 
      id: 6, 
      name: 'child2.2', 
      children: [ 
       { id: 7, name: 'subsub' } 
      ] 
      } 
     ] 
     } 
    ]; 
    } 

ngOnInit() { 
this.route.params.subscribe(params => { 
    this.courseId = params['id']; 
    this.getCourseDetails(); 
}); 
} 

HTML:

<tree-root [nodes]="tree"></tree-root> 

相信沒有錯誤的語法時才我可以看到root1root2

謝謝。

+0

你在哪裏調用'getCourseDetails()'? – ronenmiller

回答

0

我相信你需要在你的構造函數或ngOnInit()中調用getCourseDetails()這應該作爲類聲明的狀態來實現。

+0

這不是必需的函數被調用。正如我所說的,語法可能不是問題,因爲樹組件被調用,我可以看到顯示的根節點。問題是孩子沒有被追加。 –

+0

爲什麼你會看到它,如果你不調用構造函數? – ronenmiller

+0

對不起,我已經調用了代碼中添加的ngOnInit代碼。 –