简体   繁体   English

Angular BroadcastChannel:在两个浏览器选项卡之间发送数据,“BroadcastChannel”类型上不存在属性

[英]Angular BroadcastChannel: Send Data between Two Browser Tabs, Property does not exist on type 'BroadcastChannel'

How do I use Broadcast channel with Angular Typescript?如何将广播频道与 Angular Typescript 一起使用? We are sending data between two browser windows, (this is per UX design).我们在两个浏览器 windows 之间发送数据(这是每个 UX 设计)。 Getting error below.下面出现错误。 Why doesn't the property exist?为什么财产不存在?

Trying to receive data in second component continuously, anytime something is sent.尝试在第二个组件中连续接收数据,无论何时发送某些东西。

Reading this resource: https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API阅读此资源: https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API

Sender Component is doing following:发件人组件正在执行以下操作:

ngOnInit() {
    const bc = new BroadcastChannel('test_channel');
    bc.postMessage('GetData');

Receiving Component:接收组件:

ngOnInit() {
  const bc = new BroadcastChannel('test_channel');
  bc.onmessage = function (ev) { 
    if (ev.data == "GetData") {
       this.productName = "test;
    }
  }
}

Error: Property 'productName ' does not exist on type 'BroadcastChannel'.错误:“BroadcastChannel”类型上不存在属性“productName”。

The issue you're having is the value of this .您遇到的问题是this的值。 In the function which you have assigned to bc.onmessage , the value of this is the function, not the surrounding class/instance.在您分配给bc.onmessage的 function 中,此值是 function,而不是周围的类/实例。 The easiest way to have a function whilst having the value of this as the instance is to use an arrow function.拥有 function 同时将其this作为实例的最简单方法是使用箭头 function。 ie IE

ngOnInit() {
    const bc = new BroadcastChannel('test_channel');
    bc.onmessage = (ev) => { 
    if (ev.data == "GetData") {
       this.productName = "test;
    }
}

That should fix the error.那应该可以解决错误。

However, I would like to point out that whilst the above would work, I wouldn't consider the BroadcastChannel API as an appropriate way of passing data between components.但是,我想指出,虽然上述方法可行,但我不会将 BroadcastChannel API 视为在组件之间传递数据的适当方式。 There are quite a few better ways of doing it:有很多更好的方法来做到这一点:

  • If one of the components is the parent of another, you should use angular's template @Input and @Output bindings ( https://angular.io/guide/inputs-outputs#input-and-output-properties .如果其中一个组件是另一个组件的父级,则应使用 Angular 的模板 @Input 和 @Output 绑定( https://angular.io/guide/inputs-outputs#input-and-output-properties
  • If the components are separate but share data, you should use a service.如果组件是独立的但共享数据,则应使用服务。 As far as I understand it, it is best practice to use components to display things, but keep data and data processing in services.据我了解,最好的做法是使用组件来显示事物,但将数据和数据处理保留在服务中。 ( https://angular.io/guide/singleton-services ) https://angular.io/guide/singleton-services
  • If your intension is to use the BroadcastChannel API to pass data between tabs/windows which are open at the same origin (as it is intended for), it would be better to have a service that handles the usage of the API and cross-window communication, and have the components use services.如果您的意图是使用 BroadcastChannel API 在同一来源打开的选项卡/窗口之间传递数据(因为它的目的),最好有一个服务来处理 API 和跨窗口的使用通信,并让组件使用服务。 This is always the better option, as it encapsulates the implementation, and leaves your components to worry about displaying data, rather than getting it.这总是更好的选择,因为它封装了实现,让您的组件担心显示数据,而不是获取数据。 Then you would have one service that both sends and receives data from the channel.然后,您将拥有一项服务,该服务既可以从通道发送数据,也可以从通道接收数据。 Don't worry about it receiving data that it sends out, the API is smarter than that.不用担心它会收到它发出的数据,API 比这更聪明。 You could simplify this by using a library, such as https://github.com/pubkey/broadcast-channel#readme , with a big benefit being that it will work on older browsers, including ones that don't have the BroadcastChannel API (it uses other, slower methods as a fallback for these cases).您可以通过使用诸如https://github.com/pubkey/broadcast-channel#readme之类的库来简化此操作,最大的好处是它可以在较旧的浏览器上运行,包括那些没有 BroadcastChannel API (它使用其他较慢的方法作为这些情况的后备)。 It also means you don't have to keep with the spec, just use the library and keep it up to date.这也意味着您不必遵守规范,只需使用库并使其保持最新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM