简体   繁体   English

无法从 Polymer 3 Mixin 触发事件

[英]Can not fire a event from a Polymer 3 Mixin

I have the below in a mixin.我在mixin中有以下内容。 However, dispatchEvent is not attached to this .但是, dispatchEvent没有附加到this I get the error Cannot read property 'dispatchEvent' of undefined .我收到错误Cannot read property 'dispatchEvent' of undefined

So, I used window.dispatchEvent instead but the event doesn't get caught by the parent element.所以,我改用window.dispatchEvent但事件不会被父元素捕获。 Any ideas what I can do to fire a event in a mixin?有什么想法可以在mixin中触发事件吗?

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

You probably need to bind the method您可能需要绑定方法

Add this to your mixin:将此添加到您的 mixin 中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM