Sub topic 5
Public properties in JavaScript with @api
This chapter assumes you can create a Lightning web component bundle (HTML template, JavaScript class, js-meta.xml file) and that you know basic JavaScript class syntax. It covers how the @api decorator makes a property settable from outside the component. This is the foundation for the App Builder property panel covered in later chapters.
What @api does
By default, fields in a component's JavaScript class are private. Nothing outside the component can read or set them. The @api decorator marks a field as public. A parent component, a Lightning page, or App Builder can then set its value.
import { LightningElement, api } from 'lwc';
export default class AccountSummaryCard extends LightningElement {
@api recordId;
@api objectApiName;
@api headerText = 'Summary';
@api maxItems = 5;
}You must import api from lwc along with LightningElement. If you forget the import, the bundle fails to compile.
The initial values ('Summary' and 5) act as defaults. They apply until something outside the component sets a different value.
Two kinds of public properties
The class above has two groups of properties.
- Context properties:
recordIdandobjectApiName. On a record page, the platform fills these in automatically. You do not configure them. Later chapters cover them in detail. - Configuration properties:
headerTextandmaxItems. Nobody sets these automatically. An admin can set them in App Builder, but only if you also declare them in thejs-meta.xmlfile.
Declaring @api is necessary but not sufficient for the property panel. The metadata file controls what admins actually see.
Using the properties in the template
Public properties are available in the template like any other field. Curly braces bind them.
<template>
<lightning-card title={headerText} icon-name="standard:account">
<div class="slds-var-p-around_medium">
<p>Record Id: {recordId}</p>
<p>Object: {objectApiName}</p>
<p>Max items: {maxItems}</p>
</div>
</lightning-card>
</template>When the value of a public property changes, the template re-renders. On a Home page, recordId and objectApiName stay undefined, so the first two paragraphs show empty values. This is normal.
How a value reaches the property
Rules to remember
- Do not reassign a public property inside the component. The value belongs to whoever set it. If you need a changed version, copy it into a private field or a getter.
- Names are case sensitive.
headerTextin JavaScript is the same name you use in thejs-meta.xml<property name="headerText">element. A mismatch such asheadertextmeans the admin's value never arrives. - Types come from the metadata, not the class. App Builder passes a value of the type declared in the XML. For example,
type="Integer"gives your code a number, somaxItemsarrives as a number rather than a string. - Only
@apifields appear to the outside world. A field without@apicannot be configured, even if it is listed in the metadata file.
A common mistake
A property listed in the metadata file but missing @api in the class is not configurable. The panel may show the field, but the component ignores what the admin enters. Always check both sides together: the class declares the property, and the XML exposes it.
Check your understanding
Exercise
A teammate adds <property name="maxItems" type="Integer" /> to the js-meta.xml file, but the class declares maxItems = 5; with no decorator. An admin sets the value to 8 in App Builder. What does the component use, and how do you fix it?
Show answer
The component keeps using 5, because without @api the field is private and the value from App Builder never reaches it. Fix it by writing @api maxItems = 5; in the class. The name must match the name attribute in the XML exactly, including case.