[dom-bind :: _ createEventHandler]:未定义的侦听器方法

时间:2015-09-02 16:33:53

标签: javascript html5 dom onclick polymer-1.0

我正在尝试使用聚合物,但是在注册方法和调用它时遇到了问题,我在互联网上尝试了一切,似乎没有任何工作,我花了太多时间在这上面但没有结果,请帮助我

这是我的代码:

<dom-module id="products-list">
<template>
<template is="dom-bind">
   <iron-ajax id="ajax" auto
       handle-as="json"
       last-response="{{ajaxResponse}}"></iron-ajax>
   <div class="main">
     <template is="dom-repeat" items="[[ajaxResponse.dataResult]]">
       <paper-card heading="[[item.name]]" image="[[item.image]]" class="pb16 w100 flex layout horizontal">
            <div class="card-content">[[item.desc]]</div>
            <div class="card-actions">
                <paper-item>
                    <strong>[[item.price]]</strong>
                    <div class="flex"></div>
                    <paper-button on-click="_addToCart" raised><iron-icon icon="icons:add-shopping-cart"></iron-icon>Add to cart</paper-button>
                </paper-item>
            </div>
       </paper-card>
     </template>
 </div>
</template>
</template>
 <script>
    Polymer({
        is: "products-list",
        ready: function() {
            var baseUrl = getBaseURL();
            var token = getAccessToken();
            var namespace = getNamespace();
            var appKey = getAppKey();
            var appSecret = getAppSecret();
            var url = baseUrl + '/stream/product.json';
            var params = {
                'page' : 0,
                'size' : 25
            };
            var headers = {
                'X-Auth-Token' : token,
                'X-NAME-SPACE' : namespace,
                'X-APP-KEY' : appKey,
                'X-APP-SECRET' : appSecret,
            };
            var ajax = document.querySelector("#ajax");
            ajax.url = url;
            ajax.headers = headers;
        },
        properties: {

        },
        _addToCart: function (e) {
            console.log('You pressed button ' + e.model.item.name);
        }
    });
 </script>

  

除了单击按钮外,一切正常,我收到以下错误:

[dom-bind::_createEventHandler]: listener method `_addToCart` not defined

2 个答案:

答案 0 :(得分:1)

我认为您已经解决了问题,因此对于其他人,您只需将on-click替换为on-tap这样的事件:

<paper-button on-tap="_addToCart">...</paper-button>

答案 1 :(得分:0)

<paper-button id="addToCart" raised>
    <iron-icon icon="icons:add-shopping-cart"></iron-icon>
    Add to cart
</paper-button>

并准备好了:

this.$.addtoCart.addEventListener('click',this._addToCart.bind(this))