0

I would like to send record Id(Seminar ID dynamically from for each) from Parent LWC to Child LWC Template Code:

<lightning-tile key={seminar.Id}>   
  <lightning-layout key={seminar.Id}> 
    <lightning-layout-item >  
      <lightning-formatted-rich-text class="custom-box slds-grid img-style" data-recordid={seminar.Id} data-recordname={seminar.Name} data-schedulename={seminar.Description__c}  key={seminar.Id} value={seminar.Image__c} onclick={openModal}> 

Custom Event Created:

openModal(event) {
    this.seminarId = event.target.dataset.recordid;
    const selectedEvent = new CustomEvent('selected', **{ detail: {seminarId}**, });
    // Dispatches the event.
    this.dispatchEvent(selectedEvent);
}

Now here in detail tag I want to send seminarid to child. But I am getting error like 'SeminarId not defined. enter image description here

Please some one help me how to fix this.

1
  • Editior not supporting to add detail Template code. Sorry for Inconvienence
    – Rajesh P
    Sep 27, 2019 at 12:41

1 Answer 1

1

You need to do a small change in your code. {seminarId} needs to change with this.seminarId. Read more on this keyword in javascript here.

openModal(event) {
    this.seminarId = event.target.dataset.recordid;
    // {seminarId} needs to change with 'this.seminarId'
    const selectedEvent = new CustomEvent('selected', { detail: this.seminarId });
    // Dispatches the event.
    this.dispatchEvent(selectedEvent);
}

Note:- You need to fire an event only when you are passing data from child to parent. When you have to pass data from parent to child, you will not need to fire event.

Examples:-

  1. PASSING DATA FROM PARENT TO CHILD COMPONENT
  2. PASSING DATA FROM CHILD TO PARENT IN LWC

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .