css - Angular - 选择选项卡时重新加载页面?
问题描述
当用户单击我的 Angular 应用程序的浏览器选项卡时,有没有办法触发重新加载页面的功能?
我相信这将通过 window.location.reload() 来完成,但我不确定如何在选项卡选择时触发这一行。
我认为这可以通过使 body/html 具有“onfocus='reloadPage()'”属性来完成,但我不希望它在我在页面上移动鼠标时刷新 500 次,只是一旦将选项卡聚焦/单击时。
解决方案
我喜欢@enjoibp3 解决方案,但不是添加事件侦听器并删除它们。你最好使用@HostListener
(文档)
import { Component, HostListener } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html'
})
export class AppComponent {
@HostListener('window:focus') onFocus() {
console.log('window focus');
window.location.reload();
}
}
推荐阅读
- go - 使用反射访问golang中包的私有字段
- python - 需要 Geopandas 数据集的 FuncAnimation 结果
- javascript - package.json 中的 main/module 和条件导出有什么区别?
- python - Firebase 云函数 stderr: /usr/bin/env: 'python': 没有这样的文件或目录
- firebase - @JsonKey 如何访问 Map 的元素,颤动
- magento2 - Magento 2.4 自定义类别属性在 category->getParents() 返回的类别对象上为空
- javascript - 如何仅获取括号 [{}] 内的文本?
- c++ - C++ 将单个二进制 char 转换为 int 数字
- dart - Dart 检查 IP 是否在本地网络 IPv4 或 IPv6 之外
- python - python多重赋值似乎不会一次发生