Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用HostListener在angular 8中关闭浏览器来调用事件或API,_Angular_Typescript - Fatal编程技术网

如何使用HostListener在angular 8中关闭浏览器来调用事件或API,

如何使用HostListener在angular 8中关闭浏览器来调用事件或API,,angular,typescript,Angular,Typescript,我需要在关闭浏览器时调用一个简单的websocket clousure API。在我的项目中,我尝试使用HostListener,但它不起作用。下面是代码 app.component.html 语法不正确,请使用: @HostListener("window:beforeunload", ["$event"]) beforeunloadHandler(event) { console.log("closing"); // rest of your code } 不能

我需要在关闭浏览器时调用一个简单的websocket clousure API。在我的项目中,我尝试使用HostListener,但它不起作用。下面是代码

app.component.html
语法不正确,请使用:

  @HostListener("window:beforeunload", ["$event"])
  beforeunloadHandler(event) {
    console.log("closing");
    // rest of your code
  }

不能在另一个方法中声明方法。

它不起作用。。关闭浏览器后,如何查看控制台
import { HostListener,Component,OnInit  } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';
ngOnInit() {
      @c('window:beforeunlosd', ['$event'])
    beforeunloadHandler(event){

    }
  }
}
  @HostListener("window:beforeunload", ["$event"])
  beforeunloadHandler(event) {
    console.log("closing");
    // rest of your code
  }