Lightning Web Components – Wiring Apex to Property with @api Param Value

Is there a way to get result from apex method if param is @api value (that comes from parent)?

like this:

@api trip; // comes from parent
@wire(getTickets, { recordId: '$trip.Id' }) tickets;

or how to pass this param?

Thank you!

Best Answer

Define a setter-getter for tripId and use it to extract the @wire configuration value.

Here's what that looks like:

tripId; // value to use with @wire

_trip; // private var used with getter-setter
@api 
set trip(value) {
    this._trip = value;
    this.tripId = value.Id; // extract the value 
}
get trip() { return this._trip; }

@wire(getTickets, { recordId: '$tripId' }) tickets;

This approach with a getter-setter is preferred over other solutions (eg a getter only on tripId) because it'll keep this.tripId up to date whenever this.trip is changed.