首页 > 解决方案 > 在销毁角度上调用http请求

问题描述

http请求未执行

  @HostListener('window:beforeunload')
  async ngOnDestroy() {
    await this.microSitioService.cancelarTransaccion(this.tarjetaCreditoService.seguimientoEtapa).then(() => {});
  }

执行 on destroy 时我需要执行 http 请求

标签: javascriptangulartypescript

解决方案


如果您正在寻找在 Angular 应用程序被销毁时执行的事件,您可以使用PlatformRef具有OnDestroy()回调的

在 main.ts

function doSomethingOnAppDestory() {
  console.log('test');
}

platformBrowserDynamic().bootstrapModule(AppModule).then(ref => {

  // Ensure Angular destroys itself on hot reloads.
  if (window['ngRef']) {
    window['ngRef'].destroy();
  }
  window['ngRef'] = ref;

  ref.onDestroy(doSomethingOnAppDestory);

  // Otherwise, log the boot error
}).catch(err => console.error(err));

查看stackblitz 演示


推荐阅读