当 属性 更改时,聚合物元素不会触发 "on-<property>-changed" 事件

Polymer element doesn't trigger the "on-<property>-changed" event when property changes

我正在使用与更改通知相关的google-sigin element from the Polymer catalog. I want to listen to changes for the signedIn attribute outside the polymer element. The property is set to notify: true and in the Polymer documentation,它说:

When using a Polymer element with other elements or frameworks, you can manually attach an on-property-changed listener to an element to be notified of property changes, and take the necessary actions based on the new value.

我试过听 on-signIn-changedon-sign-in-changedon-property-changedsignIn-changedsign-in-changed,但是 none 这些事件是被触发。

侦听事件的代码如下所示:

document.addEventListener("DOMContentLoaded", function() {
    document.querySelector('google-signin').addEventListener('on-signIn-changed', function() {
        // never gets called
    });
});

我知道我可以监听 google-signin-success 事件(这有效,我可以看到 signedIn 已更改为 true),但我想了解为什么事件不是 triggered/what 我做错了。

我注意到 属性 被称为 signedIn 而不是 signIn。你试过了吗addEventListener('signed-in-changed'

一般来说,要将外部脚本通知到 Polymer 元素 属性 更改:

  • 确保 notify 在 属性 定义中设置为 true
  • 从外部脚本中,在元素上附加一个事件侦听器。 事件名称应为 property-name-changed,其中 property-name 是 属性 名称的破折号版本(this.firstName 触发 first-name-changed).
  • 假设事件监听器的参数名称是e,访问 e.detail.value 处的新 属性 值。

示例:

<body>
  <!-- assume x-el changes its number property somehow -->
  <x-el number="1></x-el>
  <script>
    (function(document) {
      var el = document.querySelector('x-el');
      el.addEventListener('number-changed', function(e) {
        console.log('the new value is ' + e.detail.value);
      });
    })(document);
  </script>
</body>

我现在要在文档中添加一个部分,它将记录在 --> https://www.polymer-project.org/1.0/docs/devguide/properties.html#notify-external

这是一个用于修补的 jsbin --> http://jsbin.com/risevu/edit?html,output