Expose Apex Methods to Lightning Web Components

To expose an Apex method to a Lightning web component, the method must be static and either global or public. Annotate the method with @AuraEnabled.

These types are supported for input and output.

  • Primitive—Boolean, Date, DateTime, Decimal, Double, Integer, Long, and String.
  • sObject—standard and custom sObjects are both supported.
  • Apex—an instance of an Apex class. (Most often a custom class.)
  • Collection—a collection of any of the other types.
  • An Apex inner class as a parameter or return value for an Apex method that's called by a Lightning web component isn’t supported.
  • The @NamespaceAccessible Apex annotation for an @AuraEnabled Apex method referenced from a Lightning web component isn’t supported. A Lightning web component installed from a package can’t call an Apex method from an Apex class in another package, even if both packages are in the same namespace.

The Lightning Component framework doesn’t enforce any rules about the location of Apex classes. If you’re using Salesforce DX, place Apex classes in the <app dir>/main/default/classes directory.

When you construct your Apex method, you must decide whether to pass in the parameters as primitives, or as complex data types—like an Apex class or an sObject.

The lwc-recipes repo has an apexImperativeMethodWithParams and apexImperativeMethodWithComplexParams component example that calls a method with primitives or complex parameters.

See Also