Call LWC from Aura Component

by Rijwan Mohmmed
0 comment
call-lwc-from-aura-component-techdicer

Hello friend, today we are going to discuss How to call LWC from Aura Component. We will invoke LWC component method with parameters from Aura Component.

Also check, this: Platform Events in Aura Component Salesforce

call-lwc-from-aura-component-output-techdicer
call-lwc-from-aura-component-output-techdicer

Key Highlights :

  1. We can embedded the LWC component in Aura Component.
  2. Can pass multiple parameters to LWC method.
  3. We use auraId to access the LWC component in Aura Component.
  4. Use Component.find(“auraId“) for get Lwc component.

Code:

lWCChildComp.HTML :

<template>
    <lightning-card  variant="Narrow"  title="Child Component" icon-name="standard:account">
        <div class="slds-p-horizontal_small">
            <h1>Name : {Name}</h1>
            <h1>City : {City}</h1>
        </div>
    </lightning-card>
</template>

lWCChildComp.JS :

import { LightningElement, api } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';

export default class LWCChildComp extends LightningElement {
    Name;
    City;

    @api callFromParent(paremt1, paremt2) {
        this.Name = paremt1;
        this.City = paremt2;
        this.ShowToast('Success', 'Successfully Called Aura to LWC', 'success', 'dismissable');
    }

    ShowToast(title, message, variant, mode) {
        const evt = new ShowToastEvent({
            title: title,
            message: message,
            variant: variant,
            mode: mode
        });
        this.dispatchEvent(evt);
    }
}

ParentAuraComponent.cmp :

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome" access="global" >  
    
    <aura:attribute name="name" type="String"/>
    <aura:attribute name="city" type="String"/>
    
    
    <lightning:card title="Parent Aura Component" class="slds-m-bottom_small">
        <div class="slds-p-horizontal_small">
            <lightning:input name="name" value="{! v.name }" placeholder="type name" label="Name" />
            <lightning:input name="city" value="{! v.city }" placeholder="type city" label="City" />
            <lightning:button variant="brand" label="Call LWC Method" onclick="{! c.handleLWCMethodCall }" class="slds-align_absolute-center slds-m-top_small"/>  
        </div>
    </lightning:card>
    
    <c:lWCChildComp aura:id="lWCChild"></c:lWCChildComp>
</aura:component>

ParentAuraComponentController.JS :

({
	handleLWCMethodCall : function(component, event, helper) {
        var name = component.get("v.name");
        var city = component.get("v.city");
		component.find('lWCChild').callFromParent(name, city);   
	}
})

Output:

call-lwc-from-aura-component-output-techdicer
call-lwc-from-aura-component-output-techdicer

Reference:

  1. LWC and Aura Component
What’s your Reaction?
+1
7
+1
1
+1
0
+1
1
+1
3
+1
1

You may also like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.