标签: javascript angular typescript
我是Angular v5的新手,想知道以下内容的等效内容,特别是$window.onfocus,因为它在Angular中不起作用
$window.onfocus
$window.onfocus = function() { console.log("****user attempted leaving but changed its mind, do actions here"); }
我需要在TypeScript代码中确定窗口焦点。
答案 0 :(得分:0)
使用@HostListener装饰器在您的组件中注册window.focus事件
@HostListener
window.focus
然后
@HostListener('window:focus', ['$event']) onFocus(event) { console.log("****user attempted leaving but changed its mind, do actions here"); }
工作DEMO
您可以尝试在浏览器标签中运行演示url。