Angular2加载组件动态失败

时间:2017-07-29 13:38:55

标签: angular typescript

我想在我的视图中动态加载组件,这就是我试过的

在我的html文件中

<p>
 <dynamic-component [componentData]="componentData"></dynamic-component>
</p>

然后在我的ts文件中我有

export class ChatCmpComponent implements OnInit {
 private componentData = null;

 ngOnInit() {
  this.componentData  = {
    component: ChatInitButtonComponent,
  };
}

现在收到错误

Can't bind to 'componentData' since it isn't a known property of 'dynamic-
component'.

1. If 'dynamic-component' is an Angular component and it has 
'componentData' input, then verify that it is part of this module.

什么可能是错的,我如何动态加载组件

1 个答案:

答案 0 :(得分:0)

您需要使用ng-template并创建一个自定义指令,该指令允许您通过对ViewContainerRef

的引用来注入您的组件

有关更多信息和示例,请尝试此处:

https://angular.io/guide/dynamic-component-loader