Sub topic 2
Importing the Apex method into your component
This chapter covers the import statement that connects a component to an Apex method. It assumes you know JavaScript modules and destructuring, the structure of a Lightning Web Component (HTML template, JavaScript class, @api properties), and basic Apex and SOQL. You also need an org where you can deploy code.
What the import does
An Apex method is not visible to your component until you import it. The import gives you a JavaScript function that stands for the Apex method. You can pass that function to @wire, or you can call it yourself.
Here is the Apex class from the previous chapter:
public with sharing class ContactController {
@AuraEnabled(cacheable=true)
public static List<Contact> getContacts(Id accountId) {
return [
SELECT Id, Name, Email
FROM Contact
WHERE AccountId = :accountId
WITH USER_MODE
ORDER BY Name
LIMIT 50
];
}
}The component imports it like this:
import getContacts from '@salesforce/apex/ContactController.getContacts';Reading the import path
The path has four parts, and each one must match your Apex code exactly.
| Part | Example | Meaning |
|---|---|---|
| Scope | `@salesforce/apex` | The module namespace for Apex methods |
| Class name | `ContactController` | The Apex class, case-sensitive in practice |
| Separator | `.` | A dot between class and method |
| Method name | `getContacts` | The static method with `@AuraEnabled` |
The name on the left of from is a local binding. You can pick any valid identifier, and a different name is useful when two classes expose methods with the same name:
import getContacts from '@salesforce/apex/ContactController.getContacts';
import getAccountContacts from '@salesforce/apex/AccountService.getContacts';If your class is in a managed package, put the namespace before the class name, for example @salesforce/apex/myns.ContactController.getContacts.
Using the import in a component
Once imported, the function goes into @wire as the first argument. The second argument is an object that maps Apex parameter names to values.
import { LightningElement, api, wire } from 'lwc';
import getContacts from '@salesforce/apex/ContactController.getContacts';
export default class ContactList extends LightningElement {
@api recordId;
@wire(getContacts, { accountId: '$recordId' })
contacts;
}The key accountId must match the Apex parameter name. The value '$recordId' is a reactive reference, which a later chapter explains. The next chapter explains how @wire uses the imported function.
What the platform checks
The import is resolved when you deploy the component, not when it runs. If the class or method cannot be found, the deploy fails or the component does not load. At run time, the platform also requires that the method be static, public or global, and annotated with @AuraEnabled. Wiring needs cacheable=true as well.
Common errors
- Wrong class or method name. The import path is a string, so a typo is not caught by your editor. It shows up as a deploy or load error.
- Method not static. An instance method cannot be imported. Make it
static. - Missing
cacheable=true. A method without it cannot be used with@wire. You can still call it imperatively. - Parameter name mismatch. If the key in the wire config does not match the Apex parameter name, the method receives
nullfor that parameter. - Class not deployed or not accessible. The user's profile or permission set needs access to the Apex class, or the call fails at run time.
Exercise
A teammate writes import getContacts from '@salesforce/apex/ContactController/getContacts'; and the component fails to load. What is wrong, and what is the corrected line?
Show answer
The path uses a slash between the class name and the method name. It must use a dot. The corrected line is import getContacts from '@salesforce/apex/ContactController.getContacts';.
Exercise
Two Apex classes, AccountService and ContactController, both have a cacheable method named getContacts. How can one component import both?
Show answer
Give each import a different local name, for example import getAccountContacts from '@salesforce/apex/AccountService.getContacts'; and import getContacts from '@salesforce/apex/ContactController.getContacts';. The local name on the left of from does not have to match the Apex method name.