Use Apex in Lightning Web Components While Online

The essentials of using Apex within Lightning web components are described in “Call Apex Methods” in the Lightning Web Components Developer Guide. While Apex features behave as documented when a client device is online, there are additional features available within an offline-enabled mobile app.

To take advantage of these features, you need to know the basics of using Apex within Lightning web components. In particular, there are two different ways of calling an Apex method from a Lightning web component.

  • Reactive Apex Wires

    Reading data via a wire adapter is the “natural” way to access data in Lightning web components. To add a read-only Apex method to a Lightning web component, first import the Apex method from the @salesforce/apex module, and then use the @wire annotation to connect that method to a property or function in your component.

  • Imperative Apex

    Imperative Apex is the more traditional way to call an Apex method, as a network-based API call. Imperative Apex allows you to control exactly when the method is called. You’re in control of the invocation, rather than the framework.