1
我想知道創建動態組件的最佳方式(性能)是什麼。 我嘗試了兩個,但我無法確定我應該使用哪一個。Angular2動態組件的最佳方式
在我component.html容器中的NG-開關
@Component({
selector: 'app-component-container',
template: `<div [ngSwitch]="typeComponent">
<app-component-one *ngSwitchCase="1" [value]="someValue"></app-component-one>
<app-component-two *ngSwitchCase="2" [value]="someValue"></app-component-two>
<app-component-three *ngSwitchCase="3" [value]="someValue"></app-component-three>
</div>`
})
export class ContainerComponent implements OnInit {
private typeComponent: number;
private someValue: string;
constructor() {
this.typeComponent = 2;
this.someValue = "Hello";
}
ngOnInit() {
}
}
,或者在我的component.ts容器的組分助洗劑
@Component({
selector: 'app-component-container',
template: '<div #container></div>'
})
export class ContainerComponent implements OnInit {
@ViewChild('container', {read: ViewContainerRef}) container: ViewContainerRef;
private typeComponent: number;
private someValue: string;
constructor(private _resolver: ComponentFactoryResolver) {
this.typeComponent = 2;
this.someValue = "Hello";
}
ngOnInit() {
let childComponent: ComponentRef<any> = null;
switch (this.typeComponent) {
case 1:
childComponent = this.container.createComponent<ChildComponentOne>(this._resolver.resolveComponentFactory(ChildComponentOne));
break;
case 2:
childComponent = this.container.createComponent<ChildComponentTwo>(this._resolver.resolveComponentFactory(ChildComponentTwo));
break;
case 3:
childComponent = this.container.createComponent<ChildComponentThree>(this._resolver.resolveComponentFactory(ChildComponentThree));
break;
}
if (childComponent != null) {
childComponent.instance.value = this.someValue;
}
}
}
這是簡單的爲例,在我的應用程序有動態組件的巨大重塑。
非常感謝您的回答。
在我的第二爲例,很酷的事情是,我可以有我的childComponent一個例子,我可以很容易地操縱實例並調用方法。在我看來,這是更多的OO方式。 – Soulm8