okt*_*krk 5 angular angular5 angular-gridster2
我在使用 gridster 项目中的“ 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]();
});
});
}
}
Run Code Online (Sandbox Code Playgroud)
我将此组件 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>
Run Code Online (Sandbox Code Playgroud)
它工作正常并在开发时加载 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",
Run Code Online (Sandbox Code Playgroud)
谢谢你们。
小智 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]();
});
});
}
}
}Run Code Online (Sandbox Code Playgroud)
在你的 app-component-loader.html 中,
<ng-template dynamic-host></ng-template>Run Code Online (Sandbox Code Playgroud)
最后主机指令
import {Directive, ViewContainerRef} from '@angular/core';
@Directive({
selector: '[dynamic-host]',
})
export class DynamicHostDirective {
constructor(public viewContainerRef: ViewContainerRef){}
}Run Code Online (Sandbox Code Playgroud)
希望能帮助到你
| 归档时间: |
|
| 查看次数: |
1593 次 |
| 最近记录: |