HTML
<template>
<lightning-card title="All Contact Records">
<ul>
<template if:true={responseReceived}>
<template for:each={contacts.data} for:item="contact">
<li key={contact.id}>
<div class="slds-p-around_medium lgc-bg">
<lightning-tile label={contact.LastName}>
<p class="slds-truncate" title={contact.Phone}>{contact.Phone}</p>
</lightning-tile>
</div>
</li>
</template>
</template>
</ul>
</lightning-card>
</template>
JavaScript
import { LightningElement,wire } from 'lwc';
import getAllContacts from '@salesforce/apex/ContactManager.getContact'
export default class FetchContactViaApex extends LightningElement {
@wire(getAllContacts) contacts;
get responseReceived()
{
if(this.contacts){
return true;
}
return false;
}
}
Apex Class
@AuraEnabled must be used.
The method must be public or global.
The method must be static.
public with sharing class ContactManager {
@AuraEnabled(cacheable=true)
public static List<Contact> getContact(){
return [SELECT Id, LastName, Phone FROM Contact];
}
}