我有以下指令。
在 ngOnInit 中,this.word 和 this.components 都是“未定义的”。
谁能解释我为什么?
import { Directive , Input, Output, ViewContainerRef, ComponentRef, DynamicComponentLoader, EventEmitter, OnInit, NgModule} from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { Widget } from '../widget.model';
import { CommentComponent } from './comment.component';
@Directive({selector: 'widget-factory'})
export class WidgetFactory {
@Input() name: any;
@Input() id: Number;
@Output() loaded = new EventEmitter();
private components: { 'Comment': CommentComponent };
private word: "hello";
constructor(
private _dcl: DynamicComponentLoader,
private _elementRef: ViewContainerRef
) {
}
ngOnInit() {
// Get the current element
console.log(this.word);
let component: any;
//if(this[name] === 'Comment') {
component = CommentComponent
//}
this._dcl.loadNextToLocation(CommentComponent, this._elementRef);
console.log(this.name);
}
}
深受启发: 在 Angular 2 中输出组件数组