Sub topic 5
How the template and class work together
This chapter assumes you can read basic HTML and JavaScript classes, and that you know the bundle layout and naming rules from earlier chapters. It covers how greetingCard.html and greetingCard.js connect to each other.
The two files form one unit
The HTML file describes what the component looks like. The JavaScript class holds the data and behavior the template uses. The framework links them by name: a folder greetingCard with greetingCard.html and greetingCard.js becomes one component. You never import the template in the class or the class in the template.
Here is the pair from the example bundle.
<template>
<lightning-card title="Greeting">
<p class="greeting">Hello, {name}!</p>
</lightning-card>
</template>import { LightningElement, api } from 'lwc';
export default class GreetingCard extends LightningElement {
@api name = 'World';
}The braces {name} in the template point to the name field on the class. The text Hello, {name}! renders as Hello, World! until something sets a different value.
How a binding resolves
Inside braces you can write a property name, or a property path such as {contact.name}. You cannot write expressions like {a + b} or call functions. If you need a computed value, add a getter to the class and bind to that.
import { LightningElement, api } from 'lwc';
export default class GreetingCard extends LightningElement {
@api name = 'World';
get shoutedName() {
return this.name.toUpperCase();
}
}In the template, write {shoutedName}.
The flow below shows what happens when a value changes.
Reactivity
Fields declared in the class are reactive. When the template reads a field and your code later assigns a new value, the framework re-renders the affected parts of the DOM. You don't need a decorator for this.
There is one catch. If a field holds an object or array, the framework tracks reassignment of the field. It does not track changes made deep inside the object. To trigger a re-render, assign a new object, for example this.items = [...this.items, newItem]. For public properties, the same rule applies to the value a parent passes in.
Public and private members
The class decides what the outside world can see.
- A field or method marked
@apiis public. A parent component, or App Builder if the configuration file lists it, can set it. - A field without
@apiis private. The template can still read it, but a parent cannot set it. - A component should not change its own
@apiproperties. The parent owns those values.
A property that the template needs but that nobody outside sets should stay private.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The template shows blank text | The binding name does not match the class member, or the member is misspelled. Names are case-sensitive. | Match the name exactly. |
| A parent sets an attribute and nothing happens | The class field is missing `@api`. | Add `@api` to the field. |
| The page does not update after you push to an array | The field was mutated, not reassigned. | Assign a new array. |
| The build fails on `{a + b}` | Templates accept only property references. | Use a getter. |
| The tag `<c-greeting-card>` is not found | The folder name or file names do not match. | Check the naming rules. |
Check your understanding
Exercise
A component class has @api name = 'World'; and the template contains <p>Hello, {Name}!</p>. What does the paragraph show, and why?
Show answer
It shows no name (an empty value). Binding names are case-sensitive, so {Name} does not match the name field. Changing the binding to {name} renders Hello, World!.
Exercise
You need the template to show a user's name in upper case. Why does {name.toUpperCase()} fail, and what should you write?
Show answer
Templates allow only property references, not function calls or expressions. Add a getter such as get upperName() { return this.name.toUpperCase(); } to the class and bind {upperName} in the template.