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.

Syntax 

1public with sharing class ContactController {
2    @AuraEnabled(cacheable=true)
3    public static List<Contact> getContactList() {
4        return [
5            SELECT Id, Name, Title, Phone, Email, Picture__c
6            FROM Contact
7            WHERE Picture__c != null
8            WITH USER_MODE
9            LIMIT 10
10        ];
11    }
12}

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.

Note

Usage 

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.

Tip

See Also

Release Preview

This release is in preview. Features described here don't become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can't guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.