Angular 4无法绑定到<property>,因为它不是<component>的已知属性

Jus*_*wan 16 typescript angular2-directives angular

我正在尝试在Angular 4中创建自己的指令.但是,当将类的属性绑定到组件模板时,我得到了这个错误.

控制台错误:

Unhandled Promise rejection: Template parse errors: Can't bind to
'data' since it isn't a known property of 'tree'. ("<tree [ERROR
->][data]="data"></tree>"):
Run Code Online (Sandbox Code Playgroud)

我的tree-view-component.ts:

@Component({
    selector: 'app-tree-view',
    template: '<tree [data]="data"></tree>'
})
export class TreeViewComponent implements OnInit {

    @Input() data: any[];

    constructor() {
        this.data = [
        {
            label: 'a1',
            subs: [
                {
                    label: 'a11',
                    subs: [
                        {
                            label: 'a111',
                            subs: [
                                {
                                    label: 'a1111'
                                },
                                {
                                    label: 'a1112'
                                }
                            ]
                        },
                        {
                            label: 'a112'
                        }
                    ]
                },
                {
                    label: 'a12',
                }
            ]
         }
     ];
  }

  ngOnInit() { }

}
Run Code Online (Sandbox Code Playgroud)

这是我的完整脚本文件:https://pastebin.com/hDyX2Kjj

有没有人知道这件事?TIA

Gün*_*uer 21

每个组件,指令和管道都需要注册 @NgModule()

@NgModule({
  declarations: [TreeViewComponent]
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅


Kam*_*aja 6

为 ParentComponent 运行测试时,我遇到了同样的错误。里面是带有@Input 属性的 ChildComponent 组件:string;。这两个组件也在 app.module.ts 中声明。

我已经解决了这个问题(父组件测试文件):

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ParentComponent, ChildComponent]// added child component declaration here
    })
      .compileComponents();
  }));


Run Code Online (Sandbox Code Playgroud)