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
Key Highlights :
- We can embedded the LWC component in Aura Component.
- Can pass multiple parameters to LWC method.
- We use auraId to access the LWC component in Aura Component.
- 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:
Reference:
What’s your Reaction?
+1
7
+1
1
+1
+1
1
+1
3
+1
1