How to Communicate between LWC and Aura component ??? You are on right blog.
In this blog we are going to call LWC method from Aura component. Also we are dispatching event from LWC and capture into Aura component.
Note: We can add LWC into Aura component, but we CANNOT add Aura component into LWC.
1. Create LWC component : lwcaura
Copy paste below code into lwcaura.html
<template>
<div>
I am inside LWC comp <br/>
<lightning-button variant="brand" label="Dispatch Event" title="Dispatch Event" onclick={handleClick}></lightning-button>
</div>
</template>
Copy paste below code into lwcaura.html
import { LightningElement, api } from 'lwc';
export default class Lwcaura extends LightningElement {
@api
childCmpMethod(message, page_no){
console.log('Child Cmp method executed message- '+message);
console.log(page_no);
}
handleClick(){
const eve = new CustomEvent(
"select",
{
detail : {
message : "From LWC COmponant"
}
}
);
this.dispatchEvent(eve);
}
}
2. Create Aura component : auralwc
Copy paste below code into auralwc.cmp
<aura:component>
<lightning:card title="LWC-AURA demo" iconName="custom:custom23">
<div>
<c:lwcaura onselect="{!c.doHandle}" aura:id="childCmp"></c:lwcaura>
</div><br/>
<div>
<lightning:button label="Call CHild method" variant="brand"
onclick="{!c.callChild}"></lightning:button>
</div>
</lightning:card>
</aura:component>
3. Create Lightning App : lwcApp
Copy paste below code into it:
<aura:application extends="force:slds">
<c:auralwc></c:auralwc>
</aura:application>
Open Developer console and open lwcApp.app. Click Preview button
Click Dispatch Event button to call custom event in LWC.
Click Call Child Method button to call LWC method with parameters.
Thats It.
Enjoy! If you have any questions, comments etc. please feel free to let me know. As always, please feel free to get in touch me as I would be more than happy to assist you with any of your Salesforce development needs.
Read More: WhatsApp on Salesforce Integration in 5 Steps
Which Salesforce API Do I Use in Integration?
When To Use Salesforce REST API
When To Use Salesforce SOAP API
When To Use Salesforce Bulk API
Latest Salesforce Interview Questions and Answers:
No comments:
Post a Comment