无法在 angular2 应用程序中接收存储事件

Unable to receive storage events in angular2 application

我正在努力在我关注的应用程序的所有打开选项卡之间进行通信

但我没有收到事件

这是我的简单组件代码

import { Component, Input, OnInit, Output, EventEmitter } from '@angular/core';

@Component({
    selector: 'selector',
    template: require('./app.component.html'),
    styles: [require('./app.component.css')]
})

export class AppComponent {
 constructor(){
       // listen event
       window.addEventListener('storage', this.message_receive);
       // to trigger dummy events
        setInterval(() => {
            this.message_broadcast({a: 'rhushi'});
        }, 1000);
}
    public message_broadcast(message) {
        localStorage.setItem('message', JSON.stringify(message));
    }
    public message_receive(ev) {
        if (ev.key === 'message') {
            let message = JSON.parse(ev.newValue);
        }
    } 
}

如有错误请指正

storage 事件仅在 localStorage 值在其他选项卡中更改时触发。

在上面的代码中,您每次广播相同的 json 意味着 localStorage 中的值没有改变。仅当存储中发生值更改时才会触发存储事件。尝试发送不同的值。