Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 使用@input在另一个组件内加载组件_Angular - Fatal编程技术网

Angular 使用@input在另一个组件内加载组件

Angular 使用@input在另一个组件内加载组件,angular,Angular,这是一个有角度的4.x问题 我有一个标题组件,它在应用程序中随处可见。我希望能够传递另一个组件,以便它可以渲染它。我希望它是简单的,比如: <app-header [Component] = "myComponent" ></app-header> )但它对我不起作用 我也尝试过动态组件加载(DML)(),但也不起作用。它要求我在根模块的“entryComponents”中添加动态组件 有什么帮助吗?您应该在应用程序标题的模板中使用myComponent的选择

这是一个有角度的4.x问题

我有一个标题组件,它在应用程序中随处可见。我希望能够传递另一个组件,以便它可以渲染它。我希望它是简单的,比如:

<app-header
    [Component]  = "myComponent"
></app-header>
)但它对我不起作用

我也尝试过动态组件加载(DML)(),但也不起作用。它要求我在根模块的“entryComponents”中添加动态组件


有什么帮助吗?

您应该在
应用程序标题的模板中使用
myComponent
的选择器


让我们考虑你的<代码>标题组件<代码>,在它的模板中你应该包括你的代码> app我的组件< /> >:模板:代码> <代码>

< p>如果有人在寻找解决方案,我用如下:

@Component({
    selector       : 'app-header',
    templateUrl    : `<ng-container *ngComponentOutlet="dynamicComponent"></ng-container>`,
})
export class HeaderDesktopComponent implements OnInit, OnChanges {
    @Input() component: any     = null;

    dynamicComponent = null;

    ngOnInit() {
        if ( component ) {
            this.dynamicComponent = this.component;
        }
    }
}
关键是在模块的“entryComponents”中声明MyCustomHeaderComponent,它是模块的一部分

更新:
我还发现了一个处理带有@Input和@Output的动态组件的好包:

您的
mycompany
有一个选择器,如:
app my component
可能我的问题不够清楚。MyComponent实际上是动态的,在每个模块中都会有所不同。我找到了一种方法,但组件必须放在模块的“entryComponents”中。
<app-header [component]="myComponent"></app-header>
ngOnInit() {
    this.myComponent = MyCustomHeaderComponent;
}