Call Apex from LWC

In Aura Components you need to export apex method in apex class using @AuraEnabled annotation. In Lightning web component, you need to export as well as import apex method.


Syntax of import method ->



Now to call apex from LWC, first you need to export a method. exporting is similar to Aura Components. Method should be public/global and static and annotated with @AuraEnabled.


e.g.



Imported methods can be called using three ways:

  1. Wire a property

  2. Wire a function

  3. Call a method imperatively


1. Wire a property:

if an method is annotated with @AuraEnabled(cacheable=true), you can invoke method from js file in LWC.


e.g.


2. Wire a function: