角度4无法绑定到< property>因为它不是< component>的已知属性, [英] Angular 4 Can't bind to <property> since it isn't a known property of <component>

查看:65
本文介绍了角度4无法绑定到< property>因为它不是< component>的已知属性,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create my own directive in Angular 4. But, I got this error when bind the property of class into component template.

控制台错误:

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>"):

我的tree-view-component.ts:

My 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() { }

}

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

Here is my complete script file: https://pastebin.com/hDyX2Kjj

有人知道吗? TiA

Is there anyone know about this? TiA

推荐答案

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

Every component, directive, and pipe needs to be registered in @NgModule()

@NgModule({
  declarations: [TreeViewComponent]
})
export class AppModule {}

有关更多详细信息,请参见

For more details see

  • https://angular.io/api/core/NgModule
  • https://angular.io/guide/ngmodules
  • https://angular.io/guide/ngmodule-api

这篇关于角度4无法绑定到&lt; property&gt;因为它不是&lt; component&gt;的已知属性,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆