简体   繁体   English

Polymer 2.0:听众无法工作

[英]Polymer 2.0 : listeners not working

<dom-module id="polymer-starterkit-app">
  <template>
    <style>
        :host {
        display: block;
        }
        #box{
            width: 200px;
            height: 100px;
            border: 1px solid #000;
        }  
    </style>
    <h2>Hello, [[prop1]]!</h2>
    <paper-input label="hello">

    </paper-input>

    <div id="box" on-click="boxTap"></div>
  </template>

  <script>
    /** @polymerElement */
    class PolymerStarterkitApp extends Polymer.Element {
      static get is() { return 'polymer-starterkit-app'; }
      static get properties() {
        return {
          prop1: {
            type: String,
            value: 'polymer-starterkit-app'
          },
          listeners:{
            'click':'regular'
          },
          regular:function(){
              console.log('regular')
          }    

        };


      }
     boxTap(){
        console.log('boxTap')
     }

    }

    window.customElements.define(PolymerStarterkitApp.is, PolymerStarterkitApp);
  </script>
</dom-module>

As shown in the code above, I have tried to define a simple listener on-tap on my div with the class box but it doesn't seem to work! 如上面的代码所示,我尝试使用类框在我的div上定义一个简单的监听器,但它似乎不起作用! I think I'm using the wrong syntax. 我想我使用了错误的语法。 Also, why should we use listeners if we can simply use predefined listeners like on-click and on-tap? 另外,如果我们可以简单地使用预定义的监听器,如点击和点击,我们为什么要使用监听器呢? I would really appreciate any type of help! 我真的很感激任何类型的帮助!

Edit: I helped updating Polymer's documentation. 编辑:我帮助更新了Polymer的文档。 It's now very clear and detailed. 它现在非常清晰和详细。 https://www.polymer-project.org/2.0/docs/devguide/events#imperative-listeners Just read that and you're good. https://www.polymer-project.org/2.0/docs/devguide/events#imperative-listeners刚读完,你很好。 TL;DR: The listeners object is no more in Polymer 2.0, but there's a new way to do it. TL; DR:在Polymer 2.0中,侦听器对象不再存在,但是有一种新方法可以做到。


You could simply set them up in ready() . 你可以简单地在ready()设置它们。 There is no need to use .bind() in this case because this will be your custom element in the callback because it's the event's current target. 在这种情况下不需要使用.bind() ,因为this将是回调中的自定义元素,因为它是事件的当前目标。

ready () {
  super.ready()
  this.addEventListener('my-event', this._onMyEvent)
}

_onMyEvent (event) { /* ... */ }

If you need to listen for events on something that is not your custom element itself (eg window ), then do it the way it is shown in the Polymer documentation: 如果您需要侦听不属于您自定义元素的事件(例如window ),请按照Polymer文档中显示的方式进行操作:

constructor() {
  super();
  this._boundListener = this._myLocationListener.bind(this);
}

connectedCallback() {
  super.connectedCallback();
  window.addEventListener('hashchange', this._boundListener);
}

disconnectedCallback() {
  super.disconnectedCallback();
  window.removeEventListener('hashchange', this._boundListener);
}

Source: https://www.polymer-project.org/2.0/docs/devguide/events#imperative-listeners 资料来源: https//www.polymer-project.org/2.0/docs/devguide/events#imperative-listeners

You must create the listener manually 您必须手动创建侦听器

connectedCallback() {
    super.connectedCallback();
    this.addEventListener('click', this.regular.bind(this));
}

disconnectedCallback() {
    super.disconnetedCallback();
    this.removeEventListener('click', this.regular);
}

regular() {
    console.log('hello');
}

However, to add a listener to an element like the div, you need to add Polymer.GestureEventListeners 但是,要向div之类的元素添加侦听器,需要添加Polymer.GestureEventListeners

class PolymerStarterkitApp extends Polymer.GestureEventListeners(Polymer.Element) {

}

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

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