5

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!

2 Answers 2

6

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.

1
  • Cool !! thank you!
    – Ivan S.
    Jul 19, 2019 at 7:19
0

it works like that:

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

    connectedCallback() {
        this.tripId = this.trip.Id;
    }
2
  • 3
    This approach is not good because it does a one-time copy of the value of this.tripId. tripId is @api so the parent component may change the value at any time. Jul 17, 2019 at 15:04
  • so do i need pass 2 params? how to pass only 'trip' record and pass trip.Id to apex method?
    – Ivan S.
    Jul 18, 2019 at 8:58

You must log in to answer this question.

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