Sub topic 8
Targets and Configurable Properties
This chapter assumes you know what the js-meta.xml file is and how @api marks a public property in a component's JavaScript class. It covers two parts of that file: targets, which say where a component can be placed, and targetConfigs, which say what an admin can edit there.
Targets
A target is a named place in Salesforce where a component can appear. The <targets> list in the configuration file names those places. Common values are:
| Target | Where the component can be placed |
|---|---|
| `lightning__AppPage` | A Lightning app page |
| `lightning__RecordPage` | A record page |
| `lightning__HomePage` | The Home page |
A component is offered in Lightning App Builder only if two conditions hold. <isExposed> must be true, and at least one target must match the page being edited. If you leave out a target, the component does not appear in the builder's component list for that page type. It can still be used inside other components.
Configurable properties
A <targetConfig> applies settings to one or more targets. Its targets attribute lists the targets it covers. Inside it, each <property> adds a field to the App Builder properties panel.
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>Greeting Card</masterLabel>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__AppPage">
<property name="name" type="String" default="World" />
</targetConfig>
</targetConfigs>
</LightningComponentBundle>The name attribute must match a property in the JavaScript class that is decorated with @api:
import { LightningElement, api } from 'lwc';
export default class GreetingCard extends LightningElement {
@api name = 'World';
}When an admin types "Ada" into the panel, Salesforce sets name to "Ada" on the component instance. The template then renders the new value.
In this example, only the app page shows the name field. The record page target is listed in <targets>, but no targetConfig covers it. The component can be placed there, but the admin sees no editable properties.
Common errors
- Name mismatch. The
namein the XML must match the@apiproperty exactly, including case. Without a match, the panel field has no effect. - Missing
@api. A property without@apiis private. The panel cannot set it. - Unlisted target. A
targetConfigcan only refer to targets that appear in<targets>. Otherwise the deployment fails. - Wrong type.
typedescribes the input the admin sees, such asString,IntegerorBoolean. Choose a type that matches how your JavaScript uses the value. - Missing
isExposed. If it isfalse, the component never appears in the builder, whatever the targets say.
Some properties need no configuration. On a record page, Salesforce fills the recordId and objectApiName public properties automatically if the class declares them with @api.
Exercise
A component lists lightning__HomePage in <targets> and has isExposed set to true. Its targetConfig for the Home page declares <property name="title" type="String" />, but the class has title = 'Welcome' with no decorator. The admin can place the component, but changing the field has no effect. What is wrong, and how do you fix it?
Show answer
The class property is private because it lacks @api, so the panel cannot set it. Import api from lwc and declare @api title = 'Welcome';.