1<template>
2 <lightning-card title="Account List using for:each" icon-name="custom:custom63">
3 <div class="slds-m-around_medium">
4 <template if:true={accounts.data}>
5 <template for:each={accounts.data} for:item="acc">
6 <p key={acc.Id}>{acc.Name}</p>
7 </template>
8 </template>
9 <template if:true={accounts.error}>
10 {accounts.error}
11 </template>
12 </div>
13 </lightning-card>
14</template>
15
1<template for:each={lstAccounts} for:item="acc">
2 <p key={acc.Id} style="display:inline">
3 <lightning-badge label={acc.Name}></lightning-badge>
4 </p>
5</template>
1import { LightningElement, wire } from 'lwc';
2import getAccountList from '@salesforce/apex/AccountHelper.getAccountList';
3export default class AccountListForEachLWC extends LightningElement {
4 @wire(getAccountList) accounts;
5}
6
1public with sharing class AccountHelper {
2 @AuraEnabled(cacheable=true)
3 public static List<Account> getAccountList() {
4 return [SELECT Id, Name, Type, Rating,
5 Phone, Website, AnnualRevenue
6 FROM Account];
7 }
8}
9