Apex类
public with sharing class ContactController {
@AuraEnabled(cacheable=true)
public static List<Contact> getContactsBornAfter(Date birthDate) {
return [
SELECT Name, Title, Email, Phone
FROM Contact
WHERE Birthdate > :birthDate
WITH SECURITY_ENFORCED
];
}
}
js调用第一种
import { LightningElement, api, wire } from 'lwc';
import getContactsBornAfter from '@salesforce/apex/ContactController.getContactsBornAfter';
export default class WireApexProperty extends LightningElement {
@api minBirthDate;
@wire(getContactsBornAfter, { birthDate: '$minBirthDate' })
contacts;
}
js调用第二种
import { LightningElement, api, wire } from 'lwc';
import getContactsBornAfter from '@salesforce/apex/ContactController.getContactsBornAfter';
export default class CallApexImperative extends LightningElement {
@api minBirthDate;
handleButtonClick() {
getContactsBornAfter({ //imperative Apex call
birthDate: this.minBirthDate
})
.then(contacts => {
//code to execute if related contacts are returned successfully
})
.catch(error => {
//code to execute if related contacts are not returned successfully
});
}
}