以下の指令があります。ngOnInitプライベートメンバーの公開状況
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);
}
}
:Outputting array of components in Angular 2
'ngOnInit() '内に表示されないであろう' this.word'等という理由はありません。それらの「非公開」性質は、コードの動作に影響を与えないコンパイル時の概念にすぎません。私たちが見ていない何か他のものがなければならない。ちなみに、 'Comment'はどこに定義されていますか?あなたのコードが立っているので、これはコンパイルされないでしょう。とにかく実行すると、 'components'が初期化されたときに' ReferenceError'が得られます。 –
コメントはどこに定義されていますか?おかげで、これは実際には間違いです。それはCommentComponentであり、CommentComponentはComponentです。それが私の問題を解決しました。 しかし、変数this.wordがクラス内に表示されません – Mikou
コードを更新しました – Mikou