Call Apex Methods
Lightning web components can import methods from Apex classes. The imported methods are functions that the component can call either via @wire
or imperatively.
Before you use an Apex method, make sure that there isn’t an easier way to get the data, for example, using a base Lightning component. See Data Guidelines.
Assume that Apex methods provision immutable data, even if it's possible to mutate the data today. It's a best practice to treat all data that a component receives as immutable. To mutate the data, make a shallow copy of the objects you want to mutate. It’s important to understand this concept when working with data. See Data Flow.
To call an Apex method:
Consider several ways to call an Apex method:
When calling an Apex method, it's helpful to cache the Apex method results on the client-side. We also recommend importing references to objects and fields.
The lwc-recipes repo has several components that call Apex methods. Look for components whose names start with apex
.
Apex limits are applied per invocation of an Apex method. If your JavaScript code invokes multiple Apex methods via @wire
or imperatively, the Apex limits are applied separately to each method call.
The framework queues up Apex actions before sending them to the server. If there are more than 2,500 actions in a batched request, you may get a 413 HTTP error code. See Batching of Apex Actions.
See Also