【发布时间】:2017-05-25 18:39:44
【问题描述】:
我被动态组件的破坏所困扰。我会很感激一些提示。 这是我的根组件,它在页面上完美地添加了来自服务的一些组件:
/*Root*/
@Component({
selector: 'convertors',
template: "<div #target></div>"})
export class AppComponent {
@ViewChild('target', {read: ViewContainerRef}) target: ViewContainerRef;
private componentRef: ComponentRef<any>;
constructor(private componentFactoryResolver: ComponentFactoryResolver,
private viewContainerRef: ViewContainerRef){}
addComponent(){
let someComponent = this.service.getService("someComponent");
const factory = this.componentFactoryResolver.resolveComponentFactory(someComponent);
this.componentRef = this.target.createComponent(factory);
}}
这是我的子组件,它是由根组件添加的。它必须自毁:
@Component({
selector: 'convertors',
template: "<button (click)="deleteComponent()" >Delete</button>"})
export class someComponent{
deleteComponent(){
/*How could I implement this function?*/
}
}
如何实现 deleteComponent() 方法? 谢谢!
【问题讨论】:
-
好的,我找到了解决方案。如果有人在这个问题上需要帮助,我会回答。