1

I need to show toast from LWC service component, but I don't have and won't have any instance of LWC component. That means no class which extends LightningElement.

This service will be used in Aura components. I just want to utilize ES6 features in the service.

How can I display toast in LWC without having LightningElement?

0

The only workaround I've thought of so far, is to wrap promise error in separate Error object with showToast(A) method and then in aura components do:

        TheAPIService.searchExternalCustomers({creditCardNumber: '4111111111111111'})
            .catch(error => error.showToast($A));
|improve this answer|||||
0

Without knowing much about whats going on in your ES6, you could have a callback function that you can pass from aura.

|improve this answer|||||
0

If LWC is a service, I don't suppose it should show any toast at all - it should remain framework agnostic. Why not dispatching an event from LWC that the aura component would listen to and display the toast in aura?

|improve this answer|||||
  • It's not framework agnostic. It's a service which provides interface for external callouts through apex in Aura and LWC. It will never possibly be framework agnotstic :) The goal is that it provides the default error handling (the toast), because that will be 99.9% of the cases, with possibility to overwrite it. – pkozuchowski Dec 13 '19 at 7:29
0

Actually, I didn't use LightningElement because I couldn't find the methods on it. Turns out I was only missing @api annotation and that's it.

Still, Aura initializes before LWC so all my methods are completely useless in aura's "init" phase.

|improve this answer|||||

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.