我正在使用Angular 2
parent component
searchResultComponent
此组件显示产品列表作为搜索结果 .
模板.html:
Child Component
ProductDetailComponent
我想要做的是当用户单击div时,如果加载了现有子组件,则将销毁现有子组件并可以重新创建新的子组件 .
角度问题:路由器仅在导航到不同路径时销毁并重新创建组件 . 如果仅更新路径参数或查询参数但路径相同,则不会销毁和重新创建组件 . 要解决这个问题,这就是解决方案 . Router Navigate does not call ngOnInit when same page . 但解决方案并未完全解决问题,因此存在少量错误 .
activeRouter.params.subscribe(val=>{
//move the code from ngOnInit to here
})
My question is how to destroy a child component completely before reloading the child component when user click the button in the parent component ?
到目前为止我在父组件中尝试的内容如下所示,但总是出现类型错误:this.component未定义 . 因为子组件仅在路由更改时加载 .
component:ComponentRef;
checkingChildCom(){
if(this.component){
this.component.destroy();
}
}