Hey guys, today in this post we are going to learn about In LWC communication how to pass the multiple values from parent component to child component through component JavaScript Method in Salesforce lightning web component.
Files we used in this post example
parentCmpLwc.html | Lightning Web Component HTML | The Templae HTML file for used to write HTML element for user interface. |
parentCmpLwc.js | Lightning Web Component JavaScript | It is holding click functionality and geting the input value from child component. |
parentCmpLwc.js-meta.xml | XML Meta File | It is used for where this lightning web component you want to exposed. |
childCmpLwc.html | Lightning Web Component HTML | It is child HTML file and calling on parentCmpLwc.html. |
childCmpLwc.js | Lightning Web Component JavaScript | It is holding @api public property and passing the value into parent componet. |
lwcApp.app | Lightning Application | It is used for call the component to preview on browser.. |
Live Demo
Other related post that would you like to learn in LWC
Create Parent Component
Step 1:- Create Lightning Web Component : parentCmpLwc.html
SFDX:Lightning Web Component >> New >> parentCmpLwc.html
parentCmpLwc.html [Lightning Web Component HTML]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<template> <lightning-card> <h3 slot="title"> <lightning-icon icon-name="utility:connected_apps" size="small"></lightning-icon> Pass the multiple values from parent component to child component in LWC </h3> <div class="slds slds-p-horizontal--medium"> <lightning-icon icon-name="utility:down" size="x-small"> </lightning-icon> <span style="color:#ff0000; display:inline-block; margin-left:5px;">Parent Component</span> <div class="slds-grid slds-wrap"> <div class="slds-col slds-size_6-of-12 slds-p-horizontal--medium slds-m-bottom--small"><lightning-input type="text" label="User Name" class="userName"></lightning-input></div> <div class="slds-col slds-size_6-of-12 slds-p-horizontal--medium slds-m-bottom--small"><lightning-input type="text" label="Email" class="userEmail"></lightning-input></div> <div class="slds-col slds-size_6-of-12 slds-p-horizontal--medium slds-m-bottom--small"><lightning-input type="text" label="Phone" class="userPhone"></lightning-input></div> <div class="slds-col slds-size_6-of-12 slds-p-horizontal--medium slds-m-bottom--small"><lightning-input type="text" label="Address" class="userAddress"></lightning-input></div> <div class="slds-col slds-size_12-of-12 slds-p-horizontal--medium slds-text-align--center" > <lightning-button label="Action Method" variant="brand" size="small" onclick={handleAction}></lightning-button> </div> </div> <lightning-icon icon-name="utility:down" size="x-small"></lightning-icon> <span style="color:#ff0000; display:inline-block; margin-left:5px;">Child Component</span> <c-child-cmp-lwc></c-child-cmp-lwc> </div> </lightning-card> </template> |
Step 2:- Create Lightning Web Component : parentCmpLwc.js
SFDX:Lightning Web Component >> New >> parentCmpLwc.js
parentCmpLwc.js [LWC JavaScript File]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import { api, LightningElement } from 'lwc'; export default class ParentCmpLwc extends LightningElement { @api username; @api useremail; @api userphone; @api useraddress; handleAction(){ this.username = this.template.querySelector('.userName').value; this.useremail= this.template.querySelector('.userEmail').value; this.userphone= this.template.querySelector('.userPhone').value; this.useraddress= this.template.querySelector('.userAddress').value; var fullValueStr = { getUserName:this.username, getUserEmail:this.useremail, geUserPhone:this.userphone, getUserAddress:this.useraddress }; window.console.log(JSON.stringify('fullValueStr' + fullValueStr)); this.template.querySelector("c-child-cmp-lwc").displayChildValueAction(fullValueStr); this.username = this.template.querySelector('.userName').value=''; this.useremail= this.template.querySelector('.userEmail').value=''; this.userphone= this.template.querySelector('.userPhone').value=''; this.useraddress= this.template.querySelector('.userAddress').value=''; } } |
Step 3:- Create Lightning Web Component : parentCmpLwc.js-meta.xml
SFDX:Lightning Web Component >> New >> parentCmpLwc.js-meta.xml
parentCmpLwc.js-meta.xml [LWC Meta Data XML]
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="UTF-8"?> <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> <apiVersion>45.0</apiVersion> <isExposed>true</isExposed> <targets> <target>lightning__AppPage</target> <target>lightning__RecordPage</target> <target>lightning__HomePage</target> </targets> </LightningComponentBundle> |
Create Child Component
Step 4:- Create Lightning Web Component : childCmpLwc.html
SFDX:Lightning Web Component >> New >> childCmpLwc.html
childCmpLwc.html [Lightning Web Component HTML]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<template> <lightning-card> <table class="slds-table slds-table_cell-buffer slds-table_bordered slds-table_col-bordered" border="1" cellspacing="0" cellpadding="0" bordercolor="#ccc" style="border-collapse:collapse;"> <thead> <tr> <th>User Name</th> <th>Email</th> <th>Phone</th> <th>Address</th> </tr> </thead> <tbody> <tr> <td>{userNameVal}</td> <td>{userEmailVal}</td> <td>{userPhoneVal}</td> <td>{userAddressVal}</td> </tr> </tbody> </table> </lightning-card> </template> |
Step 5:- Create Lightning Web Component : childCmpLwc.js
SFDX:Lightning Web Component >> New >> childCmpLwc.js
childCmpLwc.js [LWC JavaScript File]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import { api, LightningElement } from 'lwc'; export default class ChildCmpLwc extends LightningElement { @api userNameVal; @api userEmailVal; @api userPhoneVal; @api userAddressVal; @api displayChildValueAction(namestr){ this.userNameVal = namestr.getUserName; this.userEmailVal = namestr.getUserEmail; this.userPhoneVal = namestr.geUserPhone; this.userAddressVal = namestr.getUserAddress; } } |
Step 6:- Create Lightning Web Component : parentCmpLwc.js-meta.xml
SFDX:Lightning Web Component >> New >> parentCmpLwc.js-meta.xml
parentCmpLwc.js-meta.xml [LWC Meta Data XML]
1 2 3 4 5 |
<?xml version="1.0" encoding="UTF-8"?> <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> <apiVersion>48.0</apiVersion> <isExposed>false</isExposed> </LightningComponentBundle> |
Create Lightning Application
Step 7:- Create Lightning Application : slwcApp.app
From Developer Console >> File >> New >> Lightning Application
lwcApp.app [Component Application File]
1 2 3 |
<aura:application extends="force:slds"> <c:parentCmpLwc/> </aura:application> |
Further post that would you like to learn in LWC