angular - 如何通过使用 HostListener 在 Angular 8 中关闭浏览器来调用事件或 API,
问题描述
我需要在我的项目中关闭浏览器时调用一个简单的 websocket clousure API,我尝试使用 HostListener,但它不起作用。下面是代码https://stackblitz.com/edit/angular-nm7uww
app.component.html
<hello name="{{ name }}"></hello>
<p>
Start editing to see some magic happen :)
</p>
app.component.ts
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
}
您不能在另一个方法中声明一个方法。
推荐阅读
- python - 我只能看到用我的脚本 pydrive 创建的文件
- h2o - 哪些损失函数与 h2o xgboost 和 gbm 中的分布相关联?
- javascript - Discord.js 提及无故中断
- reactjs - Next.js 如何在 getInitialProps 中向客户端发送 200 状态
- c - 离开函数后结构变量值被改变
- java - 使用 Gzip、deflate、br 以及如何在 Spring 中处理以单个请求发送 Xml 和图像
- regex - 转义 [] 的正则表达式是什么?
- excel - 如何在 VBA 中制作名称搜索功能?
- django - 每次刷新 VS Code 终端时 pipenv 虚拟环境名称都会消失
- javascript - 登录循环正在发生。因为 ORIGIN 缺失