コンポーネントを動的にアプリケーションに追加しようとしています。動的コンポーネントを追加中にエラーが発生する角度2
フォームコンテナというコンポーネントがあります。私は設定に従ってこのコンポーネント内に異なるフォームをロードしたい。
Googleで検索し、コンポーネントを動的に追加しようとしましたが、コンソールエラーが発生しています。 プロパティcreateComponentは未定義です。 this.includeTemplate
は未定義です。値が変数に代入されていないので、コードエラーによると正しいです。しかし、私が言及している例は同じことをしており、それは働いています。
私は何か不足していると思います。
formcontainer成分
import { Component, OnInit, Input, ElementRef, ComponentFactoryResolver, ViewChild, ViewContainerRef} from '@angular/core';
import { FORM_DIRECTIVES } from '@angular/forms';
import {ActivateAccountComponent} from '/thinkshop/widgets2/thinkshopapplication/activateaccount/activateaccount.ts'
@Component({
selector: 'form-container',
templateUrl: '/thinkshop/widgets2/thinkshopapplication/login/template/landingdisplay.html'
})
export class FormContainerComponent implements OnInit{
@ViewChild('includeTemplate', { read: ViewContainerRef })
private includeTemplate: ViewContainerRef;
private componentRef:ComponentRef<any>;
constructor(private resolver: ComponentFactoryResolver) {}
ngOnInit() : void{
let componentFactory = this.resolver.resolveComponentFactory(ActivateAccountComponent);
this.componentRef = this.includeTemplate.createComponent(componentFactory);
// this.includeTemplate is undefined
}
}
ActivateAccountComponent
import { Component} from '@angular/core';
@Component({
selector: 'activate-account',
template: `<div class="ActivateAccountContainer"> HI </div>`
})
export class ActivateAccountComponent {
constructor() {}
}
landingdisplay.html
<div id="FormContainer" #includeTemplate class="FormContainer ideolveloginform"></div>
です。 landingdisplay.htmlへのパスは正しいですか?コンポーネントにhtmlを埋め込み、動作するかどうか確認してください。 このように: テンプレート: '
' –@JasonLutz ok。私はそれを試みます。しかし、テンプレートは現在のテンプレートよりも大きくなります。私はテンプレートを別々のファイルに保管していました。 –
もう一度テストしたら別のテンプレートに入れることができますが、最初にインラインで動作するようにしたいと思っていました。テンプレートは私にはうまく見えたので、問題がその道だったかもしれないと思った。 –