Calling Aura Event in LWC

apexlightning-eventslightning-web-components

I have a simple Aura component which is registering a managed package event and then firing it.

I need to call this custom_app:LoadAppEvent from lwc component instead from an Aura component. is there a way ?

I checked the Migrate Event documentation and you can use dispatchEvent instead of loadAction.fire() but it doesn't mention anything about how can we work with an managed package event.

this.dispatchEvent(loadAction);

Aura Code to fire the event:

controller.html

 <aura:component implements="forceCommunity:availableForAllPageTypes" access="global">
    <aura:registerEvent name="LoadAppEvent" type="custom_app:LoadAppEvent"/>
         <aura:handler name="init" value="{! this }" action="{! c.onInit }" />
    </aura:component>

controller.js

onInit : function(component, event, helper) {
   var loadAction = $A.get('e.custom_app:LoadAppEvent');
   loadAction.setParams({
         param1: "param1", 
         param2: "param2" 
   });
   loadAction.fire();
}

Best Answer

You can only dispatchEvent up to the first Aura component, done via an event handler.

<!-- Parent Aura markup -->
<c:myLwcComponent onloadappevent="{!c.handleLoadAppEvent}" />

...

// Child LWC Controller code
this.dispatchEvent(new CustomEvent('loadappevent', { detail: blahblahblah }));

You can also use LMS to have a type of "Aura Application Event," if that is your intent, and the package supports it.

To communicate from the managed code to your own LWC, you must wrap the LWC in an Aura component then call an @api-enabled method or use LMS. To communicate from your own LWC to the managed package, you must use the dispatchEvent method shown above, or LMS to Aura, and have it fire the proper event.

Related Topic