BroadcastChannel 是 Angular 中的一个原生函数。它不需要导入额外的库。你只是这样声明它......
const broadcastChannel = new BroadcastChannel('demo-broadcast-channel');
你发这样的消息...
this.counter++;
broadcastChannel.postMessage({
type: 'counter',
counter: this.counter
});
}
你会像这样收到它们......
const broadcastChannel = new BroadcastChannel('demo-broadcast-channel');
this.broadcastChannel.onmessage = (message) => {
console.log('Received message', message);
}
这适用于所有 Apple 设备上除 Safari 之外的所有浏览器,我们从中获得:
ReferenceError: Can't find variable: BroadcastChannel
有什么建议么?
谢谢。