2017-09-25 85 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代码。 –