0

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();
}

1 Answer 1

1

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.

4
  • I am communicating from your own LWC to the managed package event. Both my parent and child are LWC components. So, what I understood we have to use Aura in order to communicate to the managed package event.There is no way we can do it only with LWC. Sep 13, 2021 at 17:11
  • @SFDCGeek Wait, are you trying to communicate LWC->LWC? If so, LMS would be the ideal situation.
    – sfdcfox
    Sep 13, 2021 at 17:23
  • No, I am trying to communicate b/w an LWC comp and managed package event. In Aura we use var loadAction = $A.get('e.custom_app:LoadAppEvent'); to get the event detail, but how can we get the managed pkg event detail in LWC without the use of Aura. Sep 13, 2021 at 17:53
  • 1
    @SFDCGeek There's a barrier between LWC and Aura events; LWC can only directly hit the first Aura component that is a parent. There's also no way to get an Aura event in to, or out of, any LWC. As such, if the component can be updated to use LMS, that is ideal, or you will need an Aura proxy wrapping the top-level LWC to handle the event.
    – sfdcfox
    Sep 13, 2021 at 18:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .