2016-05-05 6 views
1

"loadIntoLocation"という動的コンポーネントローダーを使用しています。しかしこれはAngular2の最新バージョンではもう利用できません。動的コンポーネントローダー(loadintolocation)の代わりにQuery.Add

このコードを変換して同じ機能を得るにはどうすればよいですか?

これをリファクタリングするには、機能を維持して、代わりに新しいQuery.readを使用しますか?

速報変更通知:

コア:Query.readを追加し、DynamicComponentLoader.loadIntoLocationを削除します。 (efbd446)

DynamicComponentLoader.loadIntoLocation has been removed. Use @ViewChild(「myVarを」、読み:ViewContainerRef)をto get hold of a ViewContainerRef at an element with variable myVar`。

DynamicComponentLoader.loadIntoLocationが削除されました。変数myVarを持つ要素でViewContainerRefを保持するには、@ViewChild( 'myVar'、read:ViewContainerRef)を使用します。その後、DynamicComponentLoader.loadNextToLocationを呼び出します。 DynamicComponentLoader.loadNextToLocationは、ElementRefの代わりにViewContainerRefを使用するようになりました。

FINAL SOLUTION

this.resolver.resolveComponent(ChildComponent).then((factory:ComponentFactory) => { 
this.compRef = this.dynamicpanel.createComponent(factory) 
// input 
this.compRef.instance.forwarddata = { name: "Teddy"} 
// output 
this.compRef.instance.emitter.subscribe(event => {  this.onChildEvent(event) }) 
}); 

答えて

1

あなたはターゲットとして使用されているViewContainerRefへの参照が必要です。次のいずれかを実行でき

constructor(private viewContainerRef:ViewContainerRef) {} 

またはViewContainerRef.createComponent(> = beta.17)

@ViewChild()

<div #target></div> 
@ViewChild('target', {read: ViewContainerRef}) target; 

を使用して、ビュー内の要素のためにそれを得るようにそれを注入

注入ComponentResolver

constructor(private resolver: ComponentResolver, 
    /* if required also 
    private viewContainerRef:ViewContainerRef */) {} 

次いでも

this.dcl.loadNextToLocation(SomeComponent, this.target).then((cmpRef) => { 
    this.cmpRef = cmpRef; 
}); 

参照ようにコンポーネントを追加

this.resolver.resolveComponent(this.type).then((factory:ComponentFactory<any>) => { 
    this.cmpRef = this.target.createComponent(factory); 
    // here you can set inputs and set up subscriptions to outputs 
    // input 
    this.cmpRef.instance.someInput = someValue; 
    // output 
    this.cmpRef.instance.someOutput.subscribe(event=>{ console.log(event) }); 
}); 

DynamicComponentLoader.loadNextToLocation()非推奨(beta.17)

ような成分を追加https://stackoverflow.com/a/36325468/217408プランカのため

+1

Günterありがとうございました! ;) – Teddy

+1

更新を見た... – Teddy

+0

"入力"と "出力"を入れるのを忘れてしまった;)私は私の投稿に追加しました – Teddy

関連する問題