polymerdom-eventspolymer-3.x

Can not fire a event from a Polymer 3 Mixin


I have the below in a mixin. However, dispatchEvent is not attached to this. I get the error Cannot read property 'dispatchEvent' of undefined.

So, I used window.dispatchEvent instead but the event doesn't get caught by the parent element. Any ideas what I can do to fire a event in a mixin?

  fetchError(response) {
      ...
      this.dispatchEvent(new CustomEvent(
        'http-error', {bubbles: true, detail: msg, composed: true}
      ));
      return Promise.reject(new Error(response.statusText));
    }

Solution

  • You probably need to bind the method

    Add this to your mixin:

    constructor() {
      super();
      this.fetchError = this.fetchError.bind(this)
    }