[英]Call ngOnInit for specific component in angular
我在 delete.component.ts 中有delete.component.ts
:
onDelete() {
//to delete data in server based on device id
this.httpService.deleteDataFromServer(this.data.apiToSend).subscribe((resp) => {
if(resp.status == 'SUCCESS'){
location.reload() <---- HERE my goal
}
})
}
我有多個組件,例如 simcard:
deleteDevice(id){
this.dialog.open(DeleteDevicePopupComponent, {
data:{
deviceType: 'SIM Card',
apiToSend: 'api/simcard?id='+id
}
})
}
當我點擊刪除時一切都很好,直接調用這個 function 並刪除數據一切都很好......
我的問題是我不想使用location.reload()
我想使用 this.ngOnInit() 在同一個組件中重新加載,但我在其他組件中,所以我如何在其他組件中調用 ngOnInit()?
圖:應用 function 刪除 ---> 在 function 的主體中應用 ngOnInit 到組件(simcard 示例)。
我嘗試將this.ngOnInit()
直接添加到同一組件中(simcard not delete.component)
:
deleteDevice(id){
this.dialog.open(DeleteDevicePopupComponent, {
data:{
deviceType: 'Camera',
apiToSend: 'api/dash_cam_device?id='+id
},
})
this.ngOnInit();
}
但不行!
看一眼
來自 '@angular/core' 的 ComponentRef;
我想你可以用它來實現這一點
將ngOninit()
中的代碼包裝到另一個方法中並從ngOninit()
調用它
ngOninit(){
this.yourMethod();
}
並在執行所需操作時從另一個組件發出事件,並在父組件中捕獲事件並調用自定義方法()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.