Let's say we have this code for a LWC:
<!-- helloExpressions.html -->
<template>
<div class="slds-m-around_medium">
<lightning-input name='firstName' label="First Name" onchange={handleChange}></lightning-input>
<lightning-input name='lastName' label="Last Name" onchange={handleChange}></lightning-input>
<p class="slds-m-top_medium">Uppercased Full Name: {uppercasedFullName}</p>
</div>
</template>
and this is the JS file:
// helloExpressions.js
import { LightningElement, track } from 'lwc';
export default class HelloExpressions extends LightningElement {
@track firstName = '';
@track lastName = '';
handleChange(event) {
const field = event.target.name;
if (field === 'firstName') {
this.firstName = event.target.value;
} else if (field === 'lastName') {
this.lastName = event.target.value;
}
}
get uppercasedFullName() {
return `${this.firstName} ${this.lastName}`.toUpperCase();
}
}
firstName and lastName have been decorated with @track. Now as per the documentation, decorating a property with @track makes it private and reactive, while to mark a property public and reactive, use @api.
So if I just change the decorator to @api for both these variables, it should work. I am referring these from the same component only. But it's not working.
UPDATE: 27th Feb -------------------- Somehow it's working today. I just changed the decorator of the variables to api.
import { LightningElement, api } from 'lwc';
export default class HelloExpressions extends LightningElement {
@api firstName = '';
@api lastName = '';
handleChange(event) {
const field = event.target.name;
if (field === 'firstName') {
this.firstName = event.target.value;
} else if (field === 'lastName') {
this.lastName = event.target.value;
}
}
get uppercasedFullName() {
return `${this.firstName} ${this.lastName}`.toUpperCase();
}
}
So it's working.