简体   繁体   English

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

[英]Angular 4 Can't bind to <property> since it isn't a known property of <component>

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

Console error: 控制台错误:

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

My tree-view-component.ts: 我的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() { }

}

Here is my complete script file: https://pastebin.com/hDyX2Kjj 这是我的完整脚本文件: https//pastebin.com/hDyX2Kjj

Is there anyone know about this? 有没有人知道这件事? TiA TIA

Every component, directive, and pipe needs to be registered in @NgModule() 每个组件,指令和管道都需要在@NgModule()注册

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

For more details see 有关详细信息,请参阅

I've the same error, when run test for ParentComponent. 在为ParentComponent运行测试时,我遇到了同样的错误。 Inside was ChildComponent component with @Input property: string;. 里面是带有@Input属性的ChildComponent组件:string;。 Both components were also declared inside app.module.ts. 这两个组件也在app.module.ts中声明。

I've fixed this like that (parent component test file): 我已修复此问题(父组件测试文件):

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


As The Aelfinn pointed out if you are using your Component across Modules you need to export it. 正如Aelfinn所指出的,如果您在模块之间使用组件,则需要将其导出。 BUT you should not import, export and declare it in the Module you want to use it, since its not part of this Module !!! 但是你不应该在你想要使用它的模块中导入,导出和声明它,因为它不是这个模块的一部分!
So suppose you have a TreeViewStuffModule which declares the TreeViewComponent and a DoSomethingWithTreeViewModule where the TreeViewComponent is used in, your declarations would be as follows: 因此,假设您有一个TreeViewStuffModule声明TreeViewComponent和一个使用TreeViewComponentDoSomethingWithTreeViewModule ,您的声明将如下所示:

@NgModule({
  declarations: [
    TreeViewComponent
  ],
  exports: [
    TreeViewComponent
  ]
})
export class TreeViewStuffModule { }

@NgModule({
  imports: [
    TreeViewStuffModule
  ]
})
export class DoSomethingWithTreeViewModule

If you are using TreeViewComponent in another module, you will need to import the component into the @NgModule as follows: 如果您在另一个模块使用TreeViewComponent,你需要将部件导入@NgModule如下:

@NgModule({
    imports: [TreeViewComponent],
    // This says that all components in this module can import TreeViewComponent
    exports: [ThisModulesComponents],
    declarations: [ThisModulesComponents]
})
export class ModuleDependentOnTreeViewComponent

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法绑定,因为它不是角度分量的已知属性 - Can't bind since it isn't a known property of angular component 无法绑定到“属性”,因为它不是“组件”的已知属性 - Can't bind to 'property' since it isn't a known property of 'component' 无法绑定到属性,因为它不是组件的已知属性 - Can't bind to property since it isn't a known property of a component 角度-无法绑定到“属性”,因为它不是“ a”的已知属性 - Angular - Can't bind to 'property' since it isn't a known property of 'a' 无法绑定,因为它不是选择器组件的已知属性 - Can't bind to since it isn't a known property of selector component 无法绑定到“ngForOf”,因为它不是组件的已知属性 - Can't bind to 'ngForOf' since it isn't a known property of component 无法绑定到属性,因为它不是Angular中“ component”的已知属性 - Can't bind to property since it isn't a known property of 'component' in Angular Angular 7 无法绑定到<property>因为它不是一个已知的属性<component> (来自导入的模块) - Angular 7 Can't bind to <property> since it isn't a known property of <component> (from imported module) Angular2 RC5:无法绑定到“属性 X”,因为它不是“子组件”的已知属性 - Angular2 RC5: Can't bind to 'Property X' since it isn't a known property of 'Child Component' Angular 6 无法绑定到“*ngIf”,因为它不是已知属性 - Angular 6 Can't bind to '*ngIf' since it isn't a known property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM