Child to Parent, Parent to Child Data Passing in Lwc
ParentComponent.cmp:
<template>
<div class="slds-m-around_small" onsimple={handleSimpleEvent}>
<p>I am inside of Parent Component</p>
<c-child-comp
message="pass message from parent compoent"
pageno="37"
></c-child-comp>
</div>
<div class="slds-m-around_large">
<lightning-button label="Change Date" variant="brand" onclick={handleClick}>
</lightning-button>
</div>
</template>
ParentComponent.js:
import { LightningElement,api } from 'lwc';
export default class ParentComp extends LightningElement {
@api messages="";
@api pageno="";
@api name="Md Sumon";
handleSimpleEvent(event){
this.messages = event.target.message;
this.pageno = event.target.pageno;
console.log("Message : "+this.messages);
console.log("page no : "+this.pageno);
}
handleClick(){
this.template.querySelector('c-child-comp').childMethod("After Api Function",120);
}
}
------------------------------------------ Child Component--------------------------------------------------------
childComponent.cmp:
<template>
<div class="slds-m-around_large">
<p>I am Inside of child Component</p>
<p>Message is : {message}</p>
<p>PageNo is : {pageno}</p>
<p>Date is : {date}</p>
<lightning-button variant="brand" label="Create Event" onclick={handleEvent}>
</lightning-button>
</div>
</template>
childComponent.js:
import { LightningElement,api, track } from 'lwc';
export default class ChildComp extends LightningElement {
@api message;
@api pageno;
@track date = new Date();
@api
childMethod(fromParent,pageNo){
this.date = new Date();
this.message = fromParent;
this.pageno=pageNo;
}
handleEvent(){
var newEvent = new CustomEvent('simple',
{
bubbles: true,
composed: false
}
);
this.dispatchEvent(newEvent);
}
}

No comments