首页 文章

聚合物自定义元素和聚合物手势

提问于
浏览
1

我想在Polymer-gestures的Polymer自定义元素上添加listener事件 .

这是我的示例代码:

- 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>

我有这样的错误

未捕获的TypeError:无法读取未定义的属性'_pgListeners'

在调试模式下,当我在my-custom-parent-element.html中添加事件监听器时,我检查了myCustomElement变量的值,它是null .

我怎么能在myCustomElement变量上添加事件监听器???

1 回答

  • 0

    以下行中的querySelector拼写错误:

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

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

相关问题