6

我在网格项目中使用“ ViewContainerRef ”进行动态组件渲染时遇到问题。

我已经实现了一个组件,它将组件引用、输入和输出作为输入,并在其内部创建这个给定的组件。

前任:<app-component-loader [compRef]="{ref: ExComponent}"><app-component-loader>

在组件加载器 onInit 回调中我有

if (this.compRef && this.compRef.ref) {
        this._componentFactory = this._componentFactoryResolver.resolveComponentFactory(this.compRef.ref);

        const instance  = this.viewContainer.createComponent(this._componentFactory, 0).instance;

        console.log(instance);

        if (this.compInputs) {
            Object.getOwnPropertyNames(this.compInputs).forEach(field => {
                instance[field] = this.compInputs[field];
            });
        }

        if (this.compOutputs) {
            Object.getOwnPropertyNames(this.compOutputs).forEach(field => {
                instance[field].subscribe(e => {
                    this.compOutputs[field]();
                });
            });
        }
    }

我正在将此组件 laoder 用于 gridster 项目组件,例如:

<gridster [options]="gridsterConfig">
            <gridster-item [item]="widget" *ngFor="let widget of board.widgets">
                <app-component-loader [compRef]="{ ref: registry.get(widget.outletComponent) }" [compInputs]="{ widget: widget }" [compOutputs]="{ removeCallback: widgetRemoveCallback.bind(this), changed: widgetChanged.bind(this) }"></app-component-loader>
            </gridster-item>
 </gridster>

它工作正常并在开发时加载 gridster 项目及其出口组件。

但是,今天我尝试使用服务器构建我的应用程序ng build --prod并部署到服务器,我发现我的组件加载器没有创建组件。

我是否缺少用于生产构建的动态组件创建的特殊内容。我对此进行了搜索,但一无所获。

在实现我的组件加载器组件之前,我正在使用ng-dynamic-component包创建此组件,并且它在开发时工作正常,但出现异常,说组件实例在生产时为 NULL 。

我认为创建渲染组件的方式不是问题,但需要帮助。

此外,我的依赖项是:

"@angular/animations": "^5.2.9",
"@angular/cdk": "^6.0.2",
"@angular/common": "^5.2.9",
"@angular/compiler": "^5.2.9",
"@angular/core": "^5.2.9",
"@angular/flex-layout": "^5.0.0-beta.14",
"@angular/forms": "^5.2.9",
"@angular/http": "^5.2.9",
"@angular/platform-browser": "^5.2.9",
"@angular/platform-browser-dynamic": "^5.2.9"

"@angular/cli": "^1.7.3",
"@angular/compiler-cli": "^5.2.9",
"@angular/language-service": "^5.2.9",

谢谢你们。

4

1 回答 1

0

这对我有用,为视图创建一个指令,如下所示,在你的 app-component-loader.ts onInIt

@ViewChild(DynamicHostDirective) 
   hostDirective: DynamicHostDirective

ngOnInIt(){
if (this.compRef && this.compRef.ref) {
        this._componentFactory = this._componentFactoryResolver.resolveComponentFactory(this.compRef.ref);

        const instance  = this.hostDirective.viewContainerRef.createComponent(this._componentFactory).instance;

        console.log(instance);

        if (this.compInputs) {
            Object.getOwnPropertyNames(this.compInputs).forEach(field => {
                instance[field] = this.compInputs[field];
            });
        }

        if (this.compOutputs) {
            Object.getOwnPropertyNames(this.compOutputs).forEach(field => {
                instance[field].subscribe(e => {
                    this.compOutputs[field]();
                });
            });
        }
    }
    }

在您的 app-component-loader.html 中,

<ng-template dynamic-host></ng-template>

finally 主机指令

import {Directive, ViewContainerRef} from '@angular/core';

@Directive({
  selector: '[dynamic-host]',
})
export class DynamicHostDirective {
constructor(public viewContainerRef: ViewContainerRef){}
}

希望能帮助到你

于 2018-05-23T22:43:25.823 回答