Sub topic 6
Configuring each target with targetConfigs
This chapter assumes you can read a basic Lightning web component bundle (HTML template, JavaScript class, js-meta.xml) and that you have written simple XML. You should also know how isExposed and targets work from earlier chapters.
What targetConfigs does
The targets element says where a component can be placed. The targetConfigs element says how the component behaves at each of those places. It holds one targetConfig child per target, or per group of targets, and each child can define:
- The properties admins can set in the App Builder property panel.
- The objects a record page component supports.
- Other target-specific settings, such as form factors, which later chapters cover.
Without targetConfigs, the component still appears in App Builder, but admins see no properties to edit.
The structure
The targets attribute on a targetConfig takes a comma-separated list of targets. Every target named there must also appear in the targets element. Properties defined inside a targetConfig apply only to the targets it names.
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>Account Summary Card</masterLabel>
<targets>
<target>lightning__AppPage</target>
<target>lightning__HomePage</target>
<target>lightning__RecordPage</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__AppPage,lightning__HomePage">
<property name="headerText" type="String" label="Header text" default="Summary" />
</targetConfig>
<targetConfig targets="lightning__RecordPage">
<objects>
<object>Account</object>
</objects>
<property name="headerText" type="String" label="Header text" default="Summary" />
<property name="maxItems" type="Integer" label="Maximum items" min="1" max="10" default="5" />
</targetConfig>
</targetConfigs>
</LightningComponentBundle>This file makes three decisions:
- App pages and home pages share one configuration with a single property,
headerText. - Record pages get their own configuration with two properties,
headerTextandmaxItems. - The
objectselement limits the record page version to Account records.
Linking properties to @api
A property element only works if the JavaScript class has a public property with the same name. The name attribute must match the @api field exactly, including case.
import { LightningElement, api } from 'lwc';
export default class AccountSummaryCard extends LightningElement {
@api recordId;
@api objectApiName;
@api headerText = 'Summary';
@api maxItems = 5;
}When an admin saves a value in the property panel, the platform sets the matching @api field on the component. The default attribute in the XML sets the value shown in the panel, while the JavaScript initializer is the fallback if the platform supplies nothing. Keep the two values the same so the component behaves the same everywhere.
Notice that recordId and objectApiName have no property element. The record page fills them in automatically, so admins never set them.
Why separate configurations matter
You could list every property under every target, but that causes two common problems.
- Irrelevant fields. A home page has no record, so a record-related property there confuses admins. Only list a property under targets where it makes sense.
- Invalid configuration. Some settings are valid for only one target. For example,
objectsis meant for record pages. Putting it in a configuration for other targets can fail deployment or have no effect.
Because headerText is defined in both configurations, you repeat it. XML has no way to share a definition, so repeating it is expected.
Checking your configuration
Deploy the bundle, then open a page in App Builder and select the component. The property panel should show only the properties for that page type.
sf project deploy start --source-dir force-app/main/default/lwc/accountSummaryCardIf a property is missing, compare its name with the @api field, then confirm that the page type is named in the targets attribute of the targetConfig.
Exercise
A component lists lightning_AppPage and lightning_RecordPage in targets. You want a property called showFooter to appear only on app pages. How do you set that up in targetConfigs, and what must the JavaScript class contain?
Show answer
Add a targetConfig with targets="lightning_AppPage" that contains a property element named showFooter (for example type="Boolean"). Do not add that property to the targetConfig for lightning_RecordPage. The class needs a public field with the exact same name, declared as @api showFooter, so the platform can set the value the admin chose.