简体   繁体   English

侦听 Web 组件上的全局事件

[英]Listen for global events on a Web Component

I have a main.js that makes a call to an API and receives a response object.我有一个调用 API 并接收响应对象的 main.js。 After the response, I want to fire an event that my Custom Web Component is listening for.在响应之后,我想触发一个我的自定义 Web 组件正在侦听的事件。

makeRequest(request).then((response) => { // NOTE: the API in question returns a Promise, thus using 'then()'
   dispatchCustomEvent(response);
});

let dispatchCustomEvent = (response) => {
    console.log('dispatchCustomEvent called', response);
    let myCustomEvent = new CustomEvent('package-ready',
        {
            bubbles: true,
            composed: true,
            detail: response
        }
    );
    return document.dispatchEvent(myCustomEvent);
}

This event works in the main document.此事件在主文档中起作用。 I've attached a listener to the main document to test but it is not heard on my custom component .我已将侦听器附加到主文档以进行测试,但在我的自定义组件上听不到它

window.customElements.define('app-list',

    class AppList extends HTMLElement {

        constructor() {
            super();

            let shadowRoot = this.attachShadow({mode: 'open'});

            this.addEventListener('package-ready', e => console.log('package-ready heard on app-list', e.detail));
            shadowRoot.addEventListener('package-ready', e => console.log('package-ready heard on app-list Shadow Root', e.detail));
        }
}

As you can see from above, I've attached a listener both to the component (with this ) and to its shadow root (for test purposes).从上面可以看出,我已经将一个侦听器附加到组件(使用this )和它的影子根(用于测试目的)。

The event is not heard on the defined web component.未在定义的 Web 组件上听到该事件。 I thought this might have something to do with the event capture phase (and maybe adding another flag to my custom event options object.我认为这可能与事件捕获阶段有关(并且可能向我的自定义事件选项对象添加另一个标志。

I'm still learning the ins and outs of Web Components and haven't figured out this piece.我还在学习 Web Components 的来龙去脉,还没有弄明白这一段。 Any help would be appreciated!任何帮助,将不胜感激!

You are dispatching the event on document .您正在分发document上的事件。 The event will never reach the component since events are not sent to every element on the page.事件永远不会到达组件,因为事件不会发送到页面上的每个元素。

In the capture phase the event goes from document down to the event that dispatched it, then the bubble phase walks the tree the other direction and goes from the element that dispatched it back towards document .在捕获阶段,事件从document向下传递到调度它的事件,然后冒泡阶段从另一个方向遍历树,并从将它调度回document的元素返回。

Either your component needs to add its event listener to document or your code would need to change to something like this:您的组件需要将其事件侦听器添加到document或者您的代码需要更改为以下内容:

 makeRequest(request).then((response) => { // NOTE: the API in question returns a Promise, thus using 'then()' dispatchCustomEvent(response); }); let dispatchCustomEvent = (response) => { console.log('dispatchCustomEvent called', response); let myCustomEvent = new CustomEvent('package-ready', { bubbles: true, composed: true, detail: response } ); document.querySelectorAll('app-list').forEach( el => { return el.dispatchEvent(myCustomEvent); } ); }

But I really would not suggest doing that.但我真的不建议这样做。 Instead, if the event is going to be dispatched on document then you should listen for it on document .相反,如果事件将在document上调度,那么您应该在document上监听它。

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

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