I'm trying to implement a small use case of CustomEvent() in lwc and it's not working. I have implemented custom events in past and I'm not sure what I missed here.
Child Comp:
HTML:
<template>
<lightning-card title="Child">
<div class="child">
<button class="slds-button slds-button_brand" onclick={handleClick}>Click Me!! </button>
</div>
</lightning-card>
</template>
JS:
import { LightningElement } from 'lwc';
export default class ChildCompDispatch extends LightningElement {
sGreetingToParent = 'Hello, Welcome to Events!! ';
handleClick(){
const pressedEvent = new CustomEvent('press',{detail : this.sGreetingToParent});
this.dispatchEvent(pressedEvent);
}
}
Parent Component
HTML
<template>
<lightning-card title ="Parent">
<div class="Parent" if:true={bShowGreeting}>
<p ><b>{sGreeting}</b></p>
</div>
<div>
<c-child-comp-dispatch onpress={showgreeting}></c-child-comp-dispatch>
</div>
</lightning-card>
</template>
JS
import { LightningElement } from 'lwc';
export default class ParentCompHandle extends LightningElement {
bShowGreeting = false;
sGreeting = '';
showGreeting(event){
this.bShowGreeting = true;
this.sGreeting = event.target.detail;
}
}
It's a pretty simple and straight-forward component. Not sure what's missing here. Anyone?