Sub topic 1
How a Component's Template and Class Fit Together
Prerequisites
This chapter assumes you know basic HTML and modern JavaScript, including classes, arrow functions, and template literals. It also assumes you have created a Lightning Web Component bundle before, meaning a folder with an HTML file, a JavaScript file, and a meta XML file. You do not need to know anything about getters or @api yet. Later chapters cover them.
The two halves of a component
Every Lightning Web Component (LWC) is built from two cooperating files that share a base name:
- The template is an HTML file wrapped in a
<template>tag. It describes what the component looks like. - The class is a JavaScript file that exports a class extending
LightningElement. It holds the data and behavior.
The meta XML file tells Salesforce where the component can be used. It does not take part in data binding, so this chapter ignores it.
The template never holds data of its own. It reads values from the class, and the class decides what those values are. This split keeps markup simple and puts logic in JavaScript, where you can test and reuse it.
A minimal pair
Here is the template for a component named hello:
<!-- hello.html -->
<template>
<p>Hello, {greeting}!</p>
</template>The curly braces {greeting} mark a binding. The framework looks for a property named greeting on the component class and prints its value at that spot.
Here is the matching class:
// hello.js
import { LightningElement } from 'lwc';
export default class Hello extends LightningElement {
greeting = 'World';
}greeting = 'World'; is a class field. Because the class extends LightningElement, the template can read this field. The rendered output is Hello, World!.
How the pieces are matched
The framework pairs the files by name. A bundle folder hello contains hello.html and hello.js. The default export of hello.js is the class that backs hello.html. If the names differ, the template is not attached to the class.
The diagram below shows the flow of data.
What the template can see
The template can read anything that is a property of the component instance. That includes:
- Class fields, such as
greeting. - Getters, which compute a value on demand. A later chapter covers them.
- Public properties marked with
@api, which a parent component can set.
It cannot see local variables inside methods, or anything that is not a property of this. If you write {name} in the template, there must be a name property on the class. If there is none, the binding renders nothing, and the page does not show an error message.
Reassigning a field re-renders
Class fields are reactive. When you assign a new value to a field, the framework detects the change and renders the template again with the new value.
// hello.js
import { LightningElement } from 'lwc';
export default class Hello extends LightningElement {
greeting = 'World';
connectedCallback() {
this.greeting = 'LWC';
}
}connectedCallback runs when the component is inserted into the page. After it reassigns greeting, the template shows Hello, LWC!. You never call a "refresh" function yourself. Later chapters explain exactly which kinds of change trigger a render.
Common mistakes
- Mismatched file names.
Hello.htmlnext tohello.jscan fail on case-sensitive systems. Use lowercase-first camelCase names for both files. - Forgetting
extends LightningElement. Without it, the class has no connection to the template. - Writing
this.in the template. In the template you write{greeting}, not{this.greeting}. - Misspelling the property.
{greting}silently renders nothing.
Exercise
A component's template contains <p>Welcome, {userName}!</p>, but the page shows "Welcome, !". The class has the field username = 'Sam';. What is wrong, and how do you fix it?
Show answer
The names differ in capitalization. JavaScript property names are case-sensitive, so userName does not match username, and the binding renders nothing. Change the template to {username} or rename the field to userName. Both must match exactly.