如何使用HostListener在angular 8中关闭浏览器来调用事件或API,
我需要在关闭浏览器时调用一个简单的websocket clousure API。在我的项目中,我尝试使用HostListener,但它不起作用。下面是代码 app.component.html如何使用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 } 不能
语法不正确,请使用:
@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
}