2

我正在尝试创建窗口事件侦听器,该侦听器必须侦听另一个选项卡中的事件,在该选项卡中打开了同一应用程序的另一个实例。
一些服务:

public validateItemAgainstServer = (item: EspApplication) => {
    ...
         window.localStorage.setItem('item', "|")
      ...
    });
  }

零件

constructor(private winRef: WindowRef) {
    winRef.nativeWindow.addEventListener('storage', function (e) {
      console.log("storage event occured");
   }, false);
    window.addEventListener('storage', function (e) {
      console.log("storage event occured");
   }, false);
  }

WinRef

import { Injectable } from '@angular/core';

function _window(): any {
   // return the global native browser window object
   return window;
}

@Injectable()
export class WindowRef {
   get nativeWindow(): any {
      return _window();
   }
}

遗憾的是,没有触发 onstorage 事件。是否可以修复此解决方案,或者对于如何在 Angular 中同步两个选项卡有更好的想法?

4

1 回答 1

0

在此处查看现场示例

  @HostListener('window:storage')
  onStorageChange() {
    console.log('change...');
  }
于 2021-12-03T12:36:02.013 回答