繁体   English   中英

在Polymer中使用原型的监听器属性监听儿童的属性更改通知程序不起作用

[英]Listening children's property change notifier using prototype's listeners property in Polymer not working

为什么这样行不通? 我试图一起使用notifylisteners 我想念什么吗?

Polymer({
  is: 'x-child',

  properties: {
    message: {
      type: String,
      value: '',
      notify: true
    }
  }
});

Polymer({
  is: 'x-dad',

  listeners: {
    'message-changed': '_onMessageReceived'
  },

  _onMessageReceived: function (e) {
    alert('child sent a message');
  }
});

情景

var d = document.createElement('x-dad');
var c = document.createElement('x-child');
d.appendChild(c);
c.message = 'new';

在devguide中它写道:

当属性更改时,该元素会触发一个非冒泡的 DOM事件,以将这些更改指示给感兴趣的主机。

https://www.polymer-project.org/1.0/docs/devguide/data-binding.html#change-notification-protocol

这意味着事件侦听器仅在x-child元素上被调用,而不在其祖先上被调用。 这可能是您所缺少的。

在您提供的方案中,Polymer应该如何知道如何将x-dad的message属性与x-child的message属性绑定?

如果在html中声明x-child以便关联这两项,则必须声明类似以下内容:

 <x-child message={{message}}></x-child> 

至于使您当前的设置正常工作,请考虑与在将父元素和子元素之间绑定值(其中使用JavaScript创建子元素)中使用的内容类似的方法

暂无
暂无

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

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