Getting access to a Polymer method inside a functi

2019-09-03 03:16发布

I use the Polymer Google Map element and I've faced with a problem while adding an event listener to the map.

    * blah blah blah *
    ready: function () {
        this.map.addEventListener('google-map-rightclick', this._rightClickOnMap);
    },
    test: function () {
        console.log(1);
    },
    _rightClickOnMap: function (event) {
        this.test();
    }
    * blah blah blah *

I've tried to bind this to the listener but JS throw me the error

this.map.addEventListener('google-map-rightclick', this._rightClickOnMap).bind(this);

1条回答
Lonely孤独者°
2楼-- · 2019-09-03 03:56

You need to call bind on the function and not on the event listener.

this.map.addEventListener('google-map-rightclick', this._rightClickOnMap.bind(this));
查看更多
登录 后发表回答