Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A small example of custom event & dispatch event where we define a custom event in child component and passing values from child component to parent component in — LWC #13

Open
vijayk3327 opened this issue Jul 12, 2023 · 0 comments
Assignees
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@vijayk3327
Copy link
Owner

In this post we are going to learn about How to define a custom event in child lightning web component and passing values from child component to parent component in — LWC

→ Get source code live demo link:-

Step 1:- Create Lightning Web Component : lwcParent.html

`

    <div class="slds-wrap slds-grid">
        <div class="slds-col slds-size_12-of-12 slds-p-horizontal--small slds-m-bottom--medium">
        
            <h3><b>lwcChild Component</b></h3>
            <c-lwc-child ondemoevent={handleChange}></c-lwc-child>

             <h3><b>lwcParent Component</b></h3>
             <b style="color:brown; margin-top:15px;">The Name Value Is:-</b> {currentVal}
        </div>
    </div>
`

Step 2:- Create Lightning Web Component : lwcParent.js
import {LightningElement,api} from 'lwc'; export default class LwcParent extends LightningElement { @api currentVal; handleChange(event){ this.currentVal=event.detail; //window.console.log('fldInp# ' + this.nameInp); } }

Step 3:- Create Lightning Web Component : lwcParent.js-meta.xml

<?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> <target>lightning__Tab</target> </targets> </LightningComponentBundle>

Step 4:- Create Lightning Web Component : lwcChild.html

<template> <div class="slds-wrap slds-grid"> <div class="slds-col slds-size_12-of-12 slds-p-horizontal--small slds-m-bottom--medium"> <lightning-input type="text" label="Name" name="name" onchange={handleChangeName}></lightning-input> </div> </div> </template>

Step 5:- Create Lightning Web Component : lwcChild.js

` import { LightningElement,api } from 'lwc';

export default class LwcChild extends LightningElement {
@api nameInp;
handleChangeName(event){
this.nameInp = event.target.value;
const myDemoEvent = new CustomEvent('demoevent',{
detail:this.nameInp
});
this.dispatchEvent(myDemoEvent);
//window.console.log('nameInpBBB' + this.nameInp);
}

}`

→ Get source code live demo link:-

@vijayk3327 vijayk3327 added documentation Improvements or additions to documentation question Further information is requested labels Jul 12, 2023
@vijayk3327 vijayk3327 self-assigned this Jul 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
Status: No status
Development

No branches or pull requests

1 participant