Salesforce | Communication between Elements in LWC | The Final Information


On this weblog, we’ll perceive the communication of parts in LWC. To create dynamic and versatile communication of parts with one another your lightning internet element is required. Since we all know that the lwc element will be nested, there are three choices for communication between parts.

Three Sorts of communication between the parts:-

  1.  Mother or father to little one communication
  2.  Little one to mum or dad communication
  3.  Communication between two separate parts

Mother or father To Little one Communication

We have to use the @api decorator to reveal the youngsters’s properties/strategies for the mum or dad to name instantly utilizing JavaScript API.

ChildComponent.js

@api
chnageMessage(msgString){
this.Message = msgstring.toUpperCase();
}
ParentComponent.js
this.template .querySelector('c-Little one-Part').changeMessage(occasion.goal.worth);

dont miss out iconDo not forget to take a look at: How do you Add LWC Component to Action Button?

Little one-to-Mother or father Communication

In Lightning Internet Elements, a customized occasion is used to speak from a little one element to a mum or dad element. With LWC we will create and ship customized occasions.

Create Occasion in ChildComponent js file:

onChnageEvent(occasion){
const selectEvent = new CustomEvent('customevent', {element: Value_To_Be_PasseD});
this.dispatchEvent(selectEvent);
}

Dealing with occasion in Mother or father Part:

ParentCmp.html
<c-child-comp oncustomevent = {handleCustomEvent}></c-child-comp>
ParentCmp.js
handleCustomEvent(occasion){
const textVal = occasion.element;
this.msg = textVal;
}

Communication between two Separate Elements

You need to use the pubsub library or the flash messaging service to speak between parts that aren’t in the identical DOM hierarchy. These strategies can be utilized to speak between sibling parts in an outer lwc element or between two lwc parts  which are dropped individually in lightning app builder.

The benefit over pubsub is that message channels aren’t restricted to a single web page. The Publish-Subscribe sample in Lightning Internet Elements is just like the App-type occasion in Aura packages.

Fireplace Occasion in One Part:

import {LightningElement,api,wire} from 'lwc';
import pubsub from 'c/pubsub' ;
import { currentPageReference } from 'lightning/navigation';
export default class LWCcommunity extends LightningElement {
@wire(currentPageReference) pageRef;
@api selectedMenu;
sendMessage(){
window.console.log('Occasion Firing.... ');
let message = {
  "message" : 'Good day PubSub'
}
pubsub.fireplace('simplevt', message);
window.console.log('Occasion Fired')
}
}

dont miss out iconTry one other superb weblog by Rahul right here: All You Need to Know About Salesforce Governor Limits

Registering occasion in One other Part:

message;
connectedCallBack(){
this.register();
}
register(){
window.console.log('occasion Registered');
punsub.register('simplevt', this.handleEvent.bind(this));
}
handleEvent(messageFromEvt){
window.console.log('occasion dealt with', messageFromEvt);
this.message = messageFromEvt ? JSON.stringify(messageFromEvt, null, 't'): 'no message payload';
}



Source link

Thanks for Reading

Enjoyed this post? Share it with your networks.

Leave a Feedback!