我想在 Angular 8 中从窗口监听“存储”事件。那么,在 Angular 中执行以下操作的最佳实践是什么?
window.addEventListener('storage', () => {
});
一种方法是使用 Renderer2,但还有更好的方法吗?
最佳答案
您可以为此使用 RxJS。来自Official documentation ,
RxJS offers a number of functions that can be used to create new observables. These functions can simplify the process of creating observables from things such as events, timers, promises, and so on.
RxJS 还使用纯函数,这将最大限度地降低出错的风险。
import { Observable, fromEvent } from 'rxjs';
var storage = Observable.fromEvent(window, 'storage').subscribe(res => console.log(res));
关于javascript - 在 Angular 8 中监听 DOM 事件的最佳实践是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59966529/