简体   繁体   English

从服务中销毁组件 Angular

[英]Destroy component from service Angular

Is it possible to destroy component from serivce?是否可以从服务中销毁组件? For example call this.component.ngOnDestroy() in service method or something similar?例如在服务方法中调用 this.component.ngOnDestroy() 或类似的东西?

No, you can't do that, it's a lifecycle hook that is called when angular destroys the component by itself.不,你不能这样做,它是一个生命周期钩子,当 angular 自行销毁组件时调用。

You can use the ngIf directive in the component and update the value in the directive using service.您可以在组件中使用ngIf指令并使用 service 更新指令中的值。 This is the way you can remove the angular component from DOM and angular destroys it automatically.这是您可以从 DOM 中删除 angular 组件和 angular 自动销毁它的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM