Sub topic 2
Exposing a child property with @api
This chapter assumes you can build a simple Lightning web component (an HTML template plus a JavaScript class). It also assumes you know modern JavaScript (classes, modules) and can use {propertyName} expressions in a template. You'll make a child component's field public, so a parent can set it.
Why fields are private by default
A field on a component class is private to that component. A parent can't see it or set it. This keeps components self-contained. A child chooses which fields to share, and everything else stays hidden.
To share a field, you decorate it with @api. A decorator is a marker placed before a field or method that changes how the framework treats it. A field marked with @api is a public property.
Declaring public properties
Import api from lwc, then put @api in front of each field you want to expose.
// taskCard.js (child)
import { LightningElement, api } from 'lwc';
export default class TaskCard extends LightningElement {
@api heading;
@api priority = 'normal';
}heading has no initial value, so it is undefined until a parent sets it. priority starts as 'normal', so the child works even if the parent never sets it. Give a public property a sensible default when one exists.
Using the property in the child template
Inside the child, a public property is used like any other field.
<!-- taskCard.html (child) -->
<template>
<article>
<h3>{heading}</h3>
<p>Priority: {priority}</p>
</article>
</template>Setting the property from the parent
The parent contains the child by using its tag. A component in the c namespace named taskCard becomes <c-task-card>. The parent sets each public property as an attribute on that tag.
<!-- taskBoard.html (parent) -->
<template>
<c-task-card heading="Write documentation" priority="high"></c-task-card>
<c-task-card heading={nextHeading}></c-task-card>
</template>The first tag uses literal strings. The second uses {nextHeading}, which reads a value from the parent's own JavaScript class. The second card doesn't set priority, so it keeps its default of 'normal'.
What @api does not expose
Only decorated members are reachable from the parent. In the example, if TaskCard had a plain field _clicks = 0, a parent couldn't set it. Setting an attribute for a field that isn't public has no effect, and the framework doesn't raise an error. This is a common source of "my value never arrives" confusion.
Checking your work
Compare the two sides when a value doesn't show up:
| Check | What to look for |
|---|---|
| Child JavaScript | The field has `@api` and `api` is imported from `lwc` |
| Parent markup | The attribute name matches the property name (covered in the next chapter) |
| Value source | `{expression}` refers to a field that exists on the parent class |
A missing import { api } stops the component from compiling, so that mistake is easy to spot. A misspelled attribute is harder to see because nothing fails.
Public properties are an interface
Once a parent depends on a public property, renaming or removing it can break that parent. Treat @api members as the component's contract. Expose only what a parent needs.
Exercise
A child class declares @api label; but the parent's <c-badge label="New"> shows nothing. The child template uses {labl}. What is wrong, and what is the fix?
Show answer
The template expression has a typo. {labl} refers to a field that doesn't exist, so nothing renders. The property label is public and receives "New" correctly. Change the template to {label}.
Exercise
Which of these fields can a parent set through an attribute: @api title;, count = 0;, or @api size = 'small';?
Show answer
title and size. Both carry @api. count has no decorator, so it is private to the child.