1

I work with trailhead unit Create a Child Component and Interact with It in Build a Bear-Tracking App with Lightning Web Components module.

The bearTile component triggers bearview event in this way:

handleOpenRecordClick() {
    const selectEvent = new CustomEvent('bearview', {
        bubbles: true
    });
    this.dispatchEvent(selectEvent);
}

in the class code there is also: @api bear;

And the top parent component gets the event having access to the bear property in the event object:

handleBearView(event) {
    // Navigate to bear record page
    this[NavigationMixin.Navigate]({
        type: 'standard__recordPage',
        attributes: {
            recordId: event.target.bear.Id,
            objectApiName: 'Bear__c',
            actionName: 'view',
        },
    });
}

I do not understand how it is possible while not declaring a new detail property of the CustomEvent instance.

Could any of you guide me on with that subject?

2

event.target is the element that dispatched the event. Therefore, anything that's an @api on the target can be accessed by any other component that can handle the event (by default, just the parent template). You don't need to specify a detail in this case. Note that @track and undecorated properties will not be accessible.

| improve this answer | |
  • It makes sense. Thank you. – Maciek Leks Apr 21 '19 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.