聚合物 2 中的铁信号交替?

Iron-signal alternate in Polymer 2?

我几乎在我的应用程序的任何地方都使用了 iron-signals。

现在我正在将我的 polymer 1 应用程序升级到 polymer 2,我发现 <iron-signals> 不再使用了。

实现相同的替代路径是什么。我基本上想在我的网络应用程序的不同页面之间传递数据。

您应该能够简单地从一个元素调度 window 上的事件并在其他元素中监听它们。

示例:

// Element 1

class FooElement extends Polymer.Element {
  connectedCallback() {
    super.connectedCallback()
  }

  ready() {
    super.ready()
    window.addEventListener('bar-was-called', e => {
      console.log(e.detail) // logs 'hello-bar'
    })
  }
}

// Element 2

class BarElement extends Polymer.Element {
  connectedCallback() {
    super.connectedCallback()
  }

  ready() {
    super.ready()
  }

  doBar() {
    window.dispatchEvent(new CustomEvent('bar-was-called', { 
      detail: 'hello-bar' 
    }))
  }
}

旁注

请记住,iron-signals 被删除是有原因的。据我所知,它促进了一种难以调试的通信架构。

来自<iron-signals> README

Note: avoid using iron-signals whenever you can use a controller (parent element) to mediate communication instead.

聚合物 1 中已弃用铁信号。

将 iron-signals 的用法替换为 iron-meta。