gpt4 book ai didi

javascript - polymer 自定义元素和 polymer 手势

转载 作者:行者123 更新时间:2023-11-28 07:39:57 25 4
gpt4 key购买 nike

我想使用 Polymer 手势在 Polymer 自定义元素上添加监听器事件。

这是我的示例代码:

- my-custom-element.html

<link rel="import" href="../polymer/polymer.html">

<polymer-element name="my-custom-element" attributes="width height color">
<template>
<canvas id="canvas" width="{{width}}" height="{{height}}" style="background-color: {{color}}" touch-action="none"></canvas>
</template>
<script>
Polymer({
width: '',
height: '',
color: ''
});
</script>
</polymer-element>

- my-custom-parent-element.html

<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../polymer-gestures/polymer-gestures.html">

<polymer-element name="my-custom-parent-element">
<template>
<my-custom-element width="300px" height="200px" color="yellow"></my-custom-element>
</template>
<script>
Polymer({
ready: function() {

this.myCustomElement = this.shadowRoot.querySelector('my-custom-element');

var events = [
// base events
'down',
'up',
'trackstart',
'track',
'trackend',
'tap',
'hold',
'holdpulse',
'release'
];

events.forEach(function(en) {
PolymerGestures.addEventListener(this.myCustomElement, en, function (inEvent) {
...
});
}
});
</script>
</polymer-element>

我有这样的错误

Uncaught TypeError: Cannot read property '_pgListeners' of undefined

在 Debug模式下,当我在 my-custom-parent-element.html 中添加事件监听器时,我检查了 myCustomElement 变量的值,结果为 null。

如何在 myCustomElement 变量上添加事件监听器???

最佳答案

以下行中的 querySelector 拼写错误:

this.myCustomElement = this.shadowRoot.querrySelector('my-custom-element');

尝试使用 this.shadowRoot.querySelector('my-custom-element');

关于javascript - polymer 自定义元素和 polymer 手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28178500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com