abc*_*234 4 polymer polymer-2.x
<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>
Run Code Online (Sandbox Code Playgroud)
如上面的代码所示,我尝试使用类框在我的div上定义一个简单的监听器,但它似乎不起作用!我想我使用了错误的语法.另外,如果我们可以简单地使用预定义的监听器,如点击和点击,我们为什么要使用监听器呢?我真的很感激任何类型的帮助!
编辑:我帮助更新了Polymer的文档.它现在非常清晰和详细.https://www.polymer-project.org/2.0/docs/devguide/events#imperative-listeners刚读完,你很好.TL; DR:在Polymer 2.0中,侦听器对象不再存在,但是有一种新方法可以做到.
你可以简单地设置它们ready()..bind()在这种情况下不需要使用,因为this它将是回调中的自定义元素,因为它是事件的当前目标.
ready () {
super.ready()
this.addEventListener('my-event', this._onMyEvent)
}
_onMyEvent (event) { /* ... */ }
Run Code Online (Sandbox Code Playgroud)
如果您需要监听不是自定义元素本身的事件(例如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);
}
Run Code Online (Sandbox Code Playgroud)
资料来源:https://www.polymer-project.org/2.0/docs/devguide/events#imperative-listeners
| 归档时间: |
|
| 查看次数: |
3176 次 |
| 最近记录: |