Communication between components in LWC
September 20, 2023
Parent To Child
- using @api method
childComp.js
import { LightningElement, track,api } from "lwc"; export default class ChildComp extends LightningElement { @track value=100; //reactive in nature //public method @api changeValue() { this.value=200; } }
childComp.html
<template> The Value is : {value} </template>
parentComp.html
<template> <!--calling child web component here--> <c-child-comp></c-child-comp> <lightning-button variant="brand" label="Submit" onclick={handleClick}></lightning-button> </template>
parentComp.js
import { LightningElement } from "lwc"; export default class ParentComp extends LightningElement { handleClick() { //firing an child method this.template.querySelector("c-child-Comp").changeValue(); } }
- using @api variable
childComp.js
import { LightningElement, track,api } from "lwc"; export default class ChildComp extends LightningElement { //public property @api name; connectedCallback(){ console.log('data from parent is::'+this.name); } }
childComp.html
<template> The Value from parent is : {name} </template>
parentComp.html
<template> <!--calling child web component here--> <c-child-comp name={dataFromParent}></c-child-comp> <!-- if you write name-from-parent={dataFromParent} then you need to write in the child js like @api nameFromParent; --> <lightning-button variant="brand" label="Submit" onclick={handleClick}></lightning-button> </template>
parentComp.js
import { LightningElement } from "lwc"; dataFromParent; export default class ParentComp extends LightningElement { handleClick() { this.dataFromParent=event.target.value; } }
Child To Parent
- Using declarative event approach
childComp.html
<template> <lightning-input type="text" value={dataFromChild} onchange={sendDataToParent} ></lightning-input> </template>
childComp.js
import { LightningElement, track,api } from "lwc"; export default class ChildComp extends LightningElement { @track dataFromChild; sendDataToParent(){ let clickedMeTest = new CustomEvent('senddata', {detail : this.dataFromChild}); this.dispatchEvent(clickedMeTest); } }
parentComp.html
<template> <!--calling child web component here--> <c-child-comp onsenddata={handleChildData}></c-child-comp> Data from child comp is :: {dataFromParent} </template>
parentComp.js
import { LightningElement } from "lwc"; dataFromParent; export default class ParentComp extends LightningElement { handleChildData() { this.dataFromParent=event.detail; } }
5
2
votes
Article Rating
Subscribe
Login
0 Comments
Oldest
Newest
Most Voted
Inline Feedbacks
View all comments
Latest Post
About Me
Welcome to an Aimer's weblog :)
Hi! Asif Parvez here, I'm from West Bengal's Howrah. I have extensive experience in Apex, Integration (REST API), LWC, ADMIN and working as Senior Salesforce Developer for Dazeworks Technologies(An iLink Digital Company). I am also a content creator and blogger.
I have three certifications(PD-I, PD-II, Salesforce Associate).
Our Visitor
Our Visitor
0
0
0
3
8
2
Users Today : 0
Users Yesterday : 0
Users Last 7 days : 12
Users Last 30 days : 41
Users This Month : 33
Users This Year : 381
Total Users : 382
Views Today :
Views Yesterday :
Views Last 7 days : 45
Views Last 30 days : 79
Views This Month : 70
Views This Year : 715
Total views : 716
Who's Online : 0
Your IP Address : 18.119.109.60
Server Time : 2024-12-23
Powered By WPS Visitor Counter