4

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!

4

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.

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

it works like that:

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

    connectedCallback() {
        this.tripId = this.trip.Id;
    }
  • 2
    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. – Kevin Venkiteswaran Jul 17 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 at 8:58

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.