从DOM中删除自定义元素时如何销毁Angular Service

时间:2019-12-27 08:06:41

标签: angular web-component custom-element

我一直在试图销毁PlatformRef。整个目的是优化应用程序内存(例如清除注入到根目录中的服务中的大量数据/订阅)。

正如社区所指出的那样,销毁平台并不是要走的路。从DOM中删除自定义元素后,如何销毁Angular服务?

  • 由于我的服务包含大量数组数据,因此,根据开发人员工具内存报告的建议,从DOM中删除WebComponent时,我需要释放其内存。

在AppModule中,使用以下方式创建WebComponent

private void BtnClick_Click(object sender, System.EventArgs e)
    {
        MyDialog dialog = new MyDialog();
        dialog.Show(SupportFragmentManager, "dialog");
    }

该项目已构建并作为WebComponent加载到另一个项目中。

想象另一个项目包含许多选项卡,例如Chrome浏览器,每个选项卡将呈现一个WebComponent。

  1. 所以当我关闭Tab A
  2. ngDoBootstrap(appRef: ApplicationRef) { if (environment.name === 'createAsWebComponent') { // ~~ For production ~~ if (!customElements.get('my-web-component')) { customElements.define('my-web-component', createCustomElement(AppComponent, { injector: this.injector })); } } else { // ~~ For dev ~~ appRef.bootstrap(AppComponent); } } 将由AppComponent调用
  3. 实现PlatformRef.destroy()
  4. 服务将被称为
  5. 然后我可以使用生命周期对服务本身进行一些必要的清理

问题是,由于PlatformRef不再可用(由于在Tab A关闭期间被销毁),在关闭后再次重新打开Tab A将无法启动。

0 个答案:

没有答案